Skip to content

Commit a836475

Browse files
authored
Merge pull request #3047 from dolthub/nicktobey/index
Detect when one index is always better than another.
2 parents 32df02c + c44d2fb commit a836475

File tree

4 files changed

+212
-99
lines changed

4 files changed

+212
-99
lines changed

enginetest/queries/index_queries.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,6 +4065,121 @@ var IndexPrefixQueries = []ScriptTest{
40654065
},
40664066
},
40674067
},
4068+
{
4069+
Name: "multiple nullable index prefixes",
4070+
SetUpScript: []string{
4071+
"create table test(pk int primary key, shared1 int, shared2 int, a3 int, a4 int, b3 int, b4 int, unique key a_idx(shared1, shared2, a3, a4), unique key b_idx(shared1, shared2, b3, b4))",
4072+
},
4073+
Assertions: []ScriptTestAssertion{
4074+
{
4075+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 = 3;",
4076+
Expected: []sql.Row{},
4077+
ExpectedIndexes: []string{"a_idx"},
4078+
},
4079+
{
4080+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 = 3;",
4081+
Expected: []sql.Row{},
4082+
ExpectedIndexes: []string{"b_idx"},
4083+
},
4084+
},
4085+
},
4086+
{
4087+
Name: "multiple non-unique index prefixes",
4088+
SetUpScript: []string{
4089+
"create table test(pk int primary key, shared1 int not null, shared2 int not null, a3 int not null, a4 int not null, b3 int not null, b4 int not null, key a_idx(shared1, shared2, a3, a4), key b_idx(shared1, shared2, b3, b4))",
4090+
},
4091+
Assertions: []ScriptTestAssertion{
4092+
{
4093+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 = 3;",
4094+
Expected: []sql.Row{},
4095+
ExpectedIndexes: []string{"a_idx"},
4096+
},
4097+
{
4098+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 > 3 and a3 < 5;",
4099+
Expected: []sql.Row{},
4100+
ExpectedIndexes: []string{"a_idx"},
4101+
},
4102+
{
4103+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 = 3;",
4104+
Expected: []sql.Row{},
4105+
ExpectedIndexes: []string{"b_idx"},
4106+
},
4107+
{
4108+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 > 3 and b3 < 5;",
4109+
Expected: []sql.Row{},
4110+
ExpectedIndexes: []string{"b_idx"},
4111+
},
4112+
},
4113+
},
4114+
{
4115+
Name: "multiple non-unique nullable index prefixes",
4116+
SetUpScript: []string{
4117+
"create table test(pk int primary key, shared1 int, shared2 int, a3 int, a4 int, b3 int, b4 int, key a_idx(shared1, shared2, a3, a4), key b_idx(shared1, shared2, b3, b4))",
4118+
},
4119+
Assertions: []ScriptTestAssertion{
4120+
{
4121+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 = 3;",
4122+
Expected: []sql.Row{},
4123+
ExpectedIndexes: []string{"a_idx"},
4124+
},
4125+
{
4126+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 > 3 and a3 < 5;",
4127+
Expected: []sql.Row{},
4128+
ExpectedIndexes: []string{"a_idx"},
4129+
},
4130+
{
4131+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 = 3;",
4132+
Expected: []sql.Row{},
4133+
ExpectedIndexes: []string{"b_idx"},
4134+
},
4135+
{
4136+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 > 3 and b3 < 5;",
4137+
Expected: []sql.Row{},
4138+
ExpectedIndexes: []string{"b_idx"},
4139+
},
4140+
},
4141+
},
4142+
{
4143+
Name: "unique and non-unique nullable index prefixes",
4144+
SetUpScript: []string{
4145+
"create table test(pk int primary key, shared1 int, shared2 int, a3 int, a4 int, b3 int, b4 int, unique key a_idx(shared1, shared2, a3, a4), key b_idx(shared1, shared2, b3, b4))",
4146+
},
4147+
Assertions: []ScriptTestAssertion{
4148+
{
4149+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 = 3;",
4150+
Expected: []sql.Row{},
4151+
ExpectedIndexes: []string{"a_idx"},
4152+
},
4153+
{
4154+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a3 > 3 and a3 < 5;",
4155+
Expected: []sql.Row{},
4156+
ExpectedIndexes: []string{"a_idx"},
4157+
},
4158+
{
4159+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 = 3;",
4160+
Expected: []sql.Row{},
4161+
ExpectedIndexes: []string{"b_idx"},
4162+
},
4163+
{
4164+
Query: "select * from test where shared1 = 1 and shared2 = 2 and b3 > 3 and b3 < 5;",
4165+
Expected: []sql.Row{},
4166+
ExpectedIndexes: []string{"b_idx"},
4167+
},
4168+
},
4169+
},
4170+
{
4171+
Name: "avoid picking an index simply because it matches more filters if those filters are not in the prefix.",
4172+
SetUpScript: []string{
4173+
"create table test(pk int primary key, shared1 int, shared2 int, a3 int, a4 int, b3 int, b4 int, unique key a_idx(shared1, a3, a4, shared2), key b_idx(shared1, shared2, b3, b4))",
4174+
},
4175+
Assertions: []ScriptTestAssertion{
4176+
{
4177+
Query: "select * from test where shared1 = 1 and shared2 = 2 and a4 = 3;",
4178+
Expected: []sql.Row{},
4179+
ExpectedIndexes: []string{"b_idx"},
4180+
},
4181+
},
4182+
},
40684183
}
40694184

40704185
var IndexQueries = []ScriptTest{

enginetest/queries/query_plans.go

Lines changed: 39 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)