Skip to content

Commit

Permalink
planner: add IGNORE_INDEX hint (#12059)
Browse files Browse the repository at this point in the history
  • Loading branch information
foreyes authored and lzmhhh123 committed Sep 12, 2019
1 parent c7518de commit 3fc05c4
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 56 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/parser v0.0.0-20190903084634-0daf3f706c76
github.com/pingcap/parser v0.0.0-20190912032624-978b8272c04e
github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible
github.com/pingcap/tipb v0.0.0-20190806070524-16909e03435e
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2/go.mod h1:QMdbTAXC
github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7/go.mod h1:xsfkWVaFVV5B8e1K9seWfyJWFrIhbtUTAD8NV1Pq3+w=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw=
github.com/pingcap/parser v0.0.0-20190903084634-0daf3f706c76 h1:q8d5NIRT/Urmb5woYWhlrMER8nDV33tjyvJMqODI2Rk=
github.com/pingcap/parser v0.0.0-20190903084634-0daf3f706c76/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v0.0.0-20190912032624-978b8272c04e h1:QeD1wC7bGElAhufSHH4JcIbs1cVdxnGWD3n3gcE5qeY=
github.com/pingcap/parser v0.0.0-20190912032624-978b8272c04e/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b h1:oS9PftxQqgcRouKhhdaB52tXhVLEP7Ng3Qqsd6Z18iY=
github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b/go.mod h1:3DlDlFT7EF64A1bmb/tulZb6wbPSagm5G4p1AlhaEDs=
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible h1:MkWCxgZpJBgY2f4HtwWMMFzSBb3+JPzeJgF3VrXE/bU=
Expand Down
6 changes: 3 additions & 3 deletions planner/core/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,22 @@ func genHintsFromPhysicalPlan(p PhysicalPlan, nodeType nodeType) (res []*ast.Tab
tbl := pp.TablePlans[0].(*PhysicalTableScan)
res = append(res, &ast.TableOptimizerHint{
QBName: generateQBName(nodeType, pp.blockOffset),
HintName: model.NewCIStr(HintIndex),
HintName: model.NewCIStr(HintUseIndex),
Tables: []ast.HintTable{{TableName: getTableName(tbl.Table.Name, tbl.TableAsName)}},
})
case *PhysicalIndexLookUpReader:
index := pp.IndexPlans[0].(*PhysicalIndexScan)
res = append(res, &ast.TableOptimizerHint{
QBName: generateQBName(nodeType, pp.blockOffset),
HintName: model.NewCIStr(HintIndex),
HintName: model.NewCIStr(HintUseIndex),
Tables: []ast.HintTable{{TableName: getTableName(index.Table.Name, index.TableAsName)}},
Indexes: []model.CIStr{index.Index.Name},
})
case *PhysicalIndexReader:
index := pp.IndexPlans[0].(*PhysicalIndexScan)
res = append(res, &ast.TableOptimizerHint{
QBName: generateQBName(nodeType, pp.blockOffset),
HintName: model.NewCIStr(HintIndex),
HintName: model.NewCIStr(HintUseIndex),
Tables: []ast.HintTable{{TableName: getTableName(index.Table.Name, index.TableAsName)}},
Indexes: []model.CIStr{index.Index.Name},
})
Expand Down
19 changes: 16 additions & 3 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ const (
HintHashAgg = "hash_agg"
// HintStreamAgg is hint enforce stream aggregation.
HintStreamAgg = "stream_agg"
// HintIndex is hint enforce using some indexes.
HintIndex = "index"
// HintUseIndex is hint enforce using some indexes.
HintUseIndex = "use_index"
// HintIgnoreIndex is hint enforce ignoring some indexes.
HintIgnoreIndex = "ignore_index"
// HintAggToCop is hint enforce pushing aggregation to coprocessor.
HintAggToCop = "agg_to_cop"
)
Expand Down Expand Up @@ -1975,7 +1977,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n
aggHints.preferAggType |= preferStreamAgg
case HintAggToCop:
aggHints.preferAggToCop = true
case HintIndex:
case HintUseIndex:
if len(hint.Tables) != 0 {
indexHintList = append(indexHintList, indexHintInfo{
tblName: hint.Tables[0].TableName,
Expand All @@ -1986,6 +1988,17 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n
},
})
}
case HintIgnoreIndex:
if len(hint.Tables) != 0 {
indexHintList = append(indexHintList, indexHintInfo{
tblName: hint.Tables[0].TableName,
indexHint: &ast.IndexHint{
IndexNames: hint.Indexes,
HintType: ast.HintIgnore,
HintScope: ast.HintForScan,
},
})
}
default:
// ignore hints that not implemented
}
Expand Down
10 changes: 6 additions & 4 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,20 +776,20 @@ func (s *testPlanSuite) TestAggToCopHint(c *C) {
warning string
}{
{
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), INDEX(t) */ sum(a) from t group by a",
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), USE_INDEX(t) */ sum(a) from t group by a",
best: "TableReader(Table(t)->HashAgg)->HashAgg",
},
{
sql: "select /*+ AGG_TO_COP(), INDEX(t) */ sum(b) from t group by b",
sql: "select /*+ AGG_TO_COP(), USE_INDEX(t) */ sum(b) from t group by b",
best: "TableReader(Table(t)->HashAgg)->HashAgg",
},
{
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), INDEX(t) */ distinct a from t group by a",
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), USE_INDEX(t) */ distinct a from t group by a",
best: "TableReader(Table(t)->HashAgg)->HashAgg->HashAgg",
warning: "[planner:1815]Optimizer Hint AGG_TO_COP is inapplicable",
},
{
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), HASH_JOIN(t1), INDEX(t1), INDEX(t2) */ sum(t1.a) from t t1, t t2 where t1.a = t2.b group by t1.a",
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), HASH_JOIN(t1), USE_INDEX(t1), USE_INDEX(t2) */ sum(t1.a) from t t1, t t2 where t1.a = t2.b group by t1.a",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t1.a,test.t2.b)->Projection->HashAgg",
warning: "[planner:1815]Optimizer Hint AGG_TO_COP is inapplicable",
},
Expand Down Expand Up @@ -888,6 +888,8 @@ func (s *testPlanSuite) TestIndexHint(c *C) {
ctx := context.Background()
for i, test := range input {
comment := Commentf("case:%v sql:%s", i, test)
se.GetSessionVars().StmtCtx.SetWarnings(nil)

stmt, err := s.ParseOneStmt(test, "", "")
c.Assert(err, IsNil, comment)

Expand Down
24 changes: 17 additions & 7 deletions planner/core/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
"name": "TestIndexHint",
"cases": [
// simple case
"select /*+ INDEX(t, c_d_e) */ * from t",
"select /*+ INDEX(t, c_d_e) */ * from t t1",
"select /*+ INDEX(t1, c_d_e) */ * from t t1",
"select /*+ INDEX(t1, c_d_e), INDEX(t2, f) */ * from t t1, t t2 where t1.a = t2.b",
"select /*+ USE_INDEX(t, c_d_e) */ * from t",
"select /*+ IGNORE_INDEX(t, c_d_e) */ c from t order by c",
"select /*+ USE_INDEX(t, c_d_e) */ * from t t1",
"select /*+ IGNORE_INDEX(t, c_d_e) */ t1.c from t t1 order by t1.c",
"select /*+ USE_INDEX(t1, c_d_e) */ * from t t1",
"select /*+ IGNORE_INDEX(t1, c_d_e) */ t1.c from t t1 order by t1.c",
"select /*+ USE_INDEX(t1, c_d_e), USE_INDEX(t2, f) */ * from t t1, t t2 where t1.a = t2.b",
"select /*+ IGNORE_INDEX(t1, c_d_e), IGNORE_INDEX(t2, f), HASH_JOIN(t1) */ * from t t1, t t2 where t1.a = t2.b",
// test multiple indexes
"select /*+ INDEX(t, c_d_e, f, g) */ * from t order by f",
"select /*+ USE_INDEX(t, c_d_e, f, g) */ * from t order by f",
// use TablePath when the hint only contains table.
"select /*+ INDEX(t) */ f from t where f > 10",
"select /*+ USE_INDEX(t) */ f from t where f > 10",
// there will be a warning instead of error when index not exist
"select /*+ INDEX(t, no_such_index) */ * from t"
"select /*+ USE_INDEX(t, no_such_index) */ * from t",
"select /*+ IGNORE_INDEX(t, no_such_index) */ * from t",
// use both use_index and ignore_index, same as index hints in sql.
"select /*+ USE_INDEX(t, c_d_e), IGNORE_INDEX(t, f) */ c from t order by c",
"select /*+ USE_INDEX(t, f), IGNORE_INDEX(t, f) */ c from t order by c",
"select /*+ USE_INDEX(t, c_d_e), IGNORE_INDEX(t, c_d_e) */ c from t order by c",
"select /*+ USE_INDEX(t, c_d_e, f), IGNORE_INDEX(t, c_d_e) */ c from t order by c"
]
},
{
Expand Down
Loading

0 comments on commit 3fc05c4

Please sign in to comment.