Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: make MVIndex be compatible with the use/force index hint #40495

Merged
merged 7 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions planner/core/indexmerge_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (ds *DataSource) generateIndexMergePath() error {
}

stmtCtx := ds.ctx.GetSessionVars().StmtCtx
isPossibleIdxMerge := len(indexMergeConds) > 0 && len(ds.possibleAccessPaths) > 1
isPossibleIdxMerge := len(indexMergeConds) > 0 && // have corresponding access conditions, and
(len(ds.possibleAccessPaths) > 1 || // (have multiple index paths, or
(len(ds.possibleAccessPaths) == 1 && isMVIndexPath(ds.possibleAccessPaths[0]))) // have a MVIndex)
sessionAndStmtPermission := (ds.ctx.GetSessionVars().GetEnableIndexMerge() || len(ds.indexMergeHints) > 0) && !stmtCtx.NoIndexMergeHint
// We current do not consider `IndexMergePath`:
// 1. If there is an index path.
Expand Down Expand Up @@ -505,7 +507,7 @@ func (ds *DataSource) generateAndPruneIndexMergePath(indexMergeConds []expressio
*/
func (ds *DataSource) generateIndexMergeOnDNF4MVIndex(normalPathCnt int, filters []expression.Expression) (mvIndexPaths []*util.AccessPath, err error) {
for idx := 0; idx < normalPathCnt; idx++ {
if ds.possibleAccessPaths[idx].IsTablePath() || ds.possibleAccessPaths[idx].Index == nil || !ds.possibleAccessPaths[idx].Index.MVIndex {
if !isMVIndexPath(ds.possibleAccessPaths[idx]) {
continue // not a MVIndex path
}

Expand Down Expand Up @@ -587,7 +589,7 @@ func (ds *DataSource) generateIndexMerge4MVIndex(normalPathCnt int, filters []ex
mvIndexPaths = append(mvIndexPaths, dnfMVIndexPaths...)

for idx := 0; idx < normalPathCnt; idx++ {
if ds.possibleAccessPaths[idx].IsTablePath() || ds.possibleAccessPaths[idx].Index == nil || !ds.possibleAccessPaths[idx].Index.MVIndex {
if !isMVIndexPath(ds.possibleAccessPaths[idx]) {
continue // not a MVIndex path
}

Expand Down Expand Up @@ -894,3 +896,7 @@ func unwrapJSONCast(expr expression.Expression) (expression.Expression, bool) {
}
return sf.GetArgs()[0], true
}

func isMVIndexPath(path *util.AccessPath) bool {
return !path.IsTablePath() && path.Index != nil && path.Index.MVIndex
}
14 changes: 13 additions & 1 deletion planner/core/testdata/index_merge_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"select /*+ use_index_merge(t, j0_1) */ * from t where (1 member of (j0->'$.path1')) and a<10",
"select /*+ use_index_merge(t, j0_1) */ * from t where (1 member of (j0->'$.XXX')) and a<10",
"select /*+ use_index_merge(t, j0_1) */ * from t where (1 member of (j0->'$.path1')) and (2 member of (j1)) and a<10",
"select /*+ use_index(t, j0_0) */ * from t where (1 member of (j0->'$.path0'))",
"select /*+ use_index(t, j0_1) */ * from t where (1 member of (j0->'$.path1')) and a<10",
"select * from t use index(j0_0) where (1 member of (j0->'$.path0'))",
"select * from t use index(j0_1) where (1 member of (j0->'$.path1')) and a<10",
"select * from t force index(j0_0) where (1 member of (j0->'$.path0'))",
"select * from t force index(j0_1) where (1 member of (j0->'$.path1')) and a<10",
"select /*+ use_index_merge(t, j1) */ * from t where (1 member of (j0->'$.path1')) and (2 member of (j1)) and a<10",
"select /*+ use_index_merge(t, j0_0) */ * from t where json_contains((j0->'$.path0'), '[1, 2, 3]')",
"select /*+ use_index_merge(t, j0_0) */ * from t where json_overlaps((j0->'$.path0'), '[1, 2, 3]')",
Expand Down Expand Up @@ -55,7 +61,13 @@
"select /*+ use_index_merge(t, idx2) */ * from t where a=1 and b=2 and ('3' member of (j->'$.str')) and c=4",
"select /*+ use_index_merge(t, idx2) */ * from t where a=1 and b=2 and ('3' member of (j->'$.str'))",
"select /*+ use_index_merge(t, idx2) */ * from t where a=1 and b=2",
"select /*+ use_index_merge(t, idx2) */ * from t where a=1"
"select /*+ use_index_merge(t, idx2) */ * from t where a=1",
"select /*+ use_index(t, idx) */ * from t where a=1 and b=2 and (3 member of (j)) and c=4",
"select * from t use index(idx) where a=1 and b=2 and (3 member of (j))",
"select /*+ use_index(t, idx) */ * from t where a=1 and b=2",
"select * from t use index(idx) where a=1",
"select * from t force index(idx) where a=1 and b=2 and (3 member of (j))",
"select * from t force index(idx) where a=1"
]
},
{
Expand Down
108 changes: 108 additions & 0 deletions planner/core/testdata/index_merge_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,63 @@
" └─TableRowIDScan 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select /*+ use_index(t, j0_0) */ * from t where (1 member of (j0->'$.path0'))",
"Plan": [
"Selection 8000.00 root json_memberof(cast(1, json BINARY), json_extract(test.t.j0, \"$.path0\"))",
"└─IndexMerge 10.00 root type: union",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:j0_0(cast(json_extract(`j0`, _utf8mb4'$.path0') as signed array)) range:[1,1], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select /*+ use_index(t, j0_1) */ * from t where (1 member of (j0->'$.path1')) and a<10",
"Plan": [
"Selection 2658.67 root json_memberof(cast(1, json BINARY), json_extract(test.t.j0, \"$.path1\"))",
"└─IndexMerge 3.32 root type: union",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:j0_1(cast(json_extract(`j0`, _utf8mb4'$.path1') as signed array)) range:[1,1], keep order:false, stats:pseudo",
" └─Selection(Probe) 3.32 cop[tikv] lt(test.t.a, 10)",
" └─TableRowIDScan 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t use index(j0_0) where (1 member of (j0->'$.path0'))",
"Plan": [
"Selection 8000.00 root json_memberof(cast(1, json BINARY), json_extract(test.t.j0, \"$.path0\"))",
"└─IndexMerge 10.00 root type: union",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:j0_0(cast(json_extract(`j0`, _utf8mb4'$.path0') as signed array)) range:[1,1], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t use index(j0_1) where (1 member of (j0->'$.path1')) and a<10",
"Plan": [
"Selection 2658.67 root json_memberof(cast(1, json BINARY), json_extract(test.t.j0, \"$.path1\"))",
"└─IndexMerge 3.32 root type: union",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:j0_1(cast(json_extract(`j0`, _utf8mb4'$.path1') as signed array)) range:[1,1], keep order:false, stats:pseudo",
" └─Selection(Probe) 3.32 cop[tikv] lt(test.t.a, 10)",
" └─TableRowIDScan 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t force index(j0_0) where (1 member of (j0->'$.path0'))",
"Plan": [
"Selection 8000.00 root json_memberof(cast(1, json BINARY), json_extract(test.t.j0, \"$.path0\"))",
"└─IndexMerge 10.00 root type: union",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:j0_0(cast(json_extract(`j0`, _utf8mb4'$.path0') as signed array)) range:[1,1], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t force index(j0_1) where (1 member of (j0->'$.path1')) and a<10",
"Plan": [
"Selection 2658.67 root json_memberof(cast(1, json BINARY), json_extract(test.t.j0, \"$.path1\"))",
"└─IndexMerge 3.32 root type: union",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:j0_1(cast(json_extract(`j0`, _utf8mb4'$.path1') as signed array)) range:[1,1], keep order:false, stats:pseudo",
" └─Selection(Probe) 3.32 cop[tikv] lt(test.t.a, 10)",
" └─TableRowIDScan 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select /*+ use_index_merge(t, j1) */ * from t where (1 member of (j0->'$.path1')) and (2 member of (j1)) and a<10",
"Plan": [
Expand Down Expand Up @@ -496,6 +553,57 @@
"├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1,1], keep order:false, stats:pseudo",
"└─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select /*+ use_index(t, idx) */ * from t where a=1 and b=2 and (3 member of (j)) and c=4",
"Plan": [
"Selection 0.00 root json_memberof(cast(3, json BINARY), test.t.j)",
"└─IndexMerge 0.00 root type: union",
" ├─IndexRangeScan(Build) 0.00 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1 2 3 4,1 2 3 4], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 0.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t use index(idx) where a=1 and b=2 and (3 member of (j))",
"Plan": [
"Selection 0.08 root json_memberof(cast(3, json BINARY), test.t.j)",
"└─IndexMerge 0.00 root type: union",
" ├─IndexRangeScan(Build) 0.00 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1 2 3,1 2 3], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 0.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select /*+ use_index(t, idx) */ * from t where a=1 and b=2",
"Plan": [
"IndexMerge 0.10 root type: union",
"├─IndexRangeScan(Build) 0.10 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1 2,1 2], keep order:false, stats:pseudo",
"└─TableRowIDScan(Probe) 0.10 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t use index(idx) where a=1",
"Plan": [
"IndexMerge 10.00 root type: union",
"├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1,1], keep order:false, stats:pseudo",
"└─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t force index(idx) where a=1 and b=2 and (3 member of (j))",
"Plan": [
"Selection 0.08 root json_memberof(cast(3, json BINARY), test.t.j)",
"└─IndexMerge 0.00 root type: union",
" ├─IndexRangeScan(Build) 0.00 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1 2 3,1 2 3], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 0.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
},
{
"SQL": "select * from t force index(idx) where a=1",
"Plan": [
"IndexMerge 10.00 root type: union",
"├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:idx(a, b, cast(`j` as signed array), c) range:[1,1], keep order:false, stats:pseudo",
"└─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo"
]
}
]
},
Expand Down