Skip to content

Commit 24c2ad7

Browse files
francis0407zz-jason
authored andcommitted
planner: choose TableScan when use an empty index hint (#12100)
1 parent 7f41ed3 commit 24c2ad7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

planner/core/logical_plans.go

+10
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ type accessPath struct {
372372
forced bool
373373
}
374374

375+
// getTablePath finds the TablePath from a group of accessPaths.
376+
func getTablePath(paths []*accessPath) *accessPath {
377+
for _, path := range paths {
378+
if path.isTablePath {
379+
return path
380+
}
381+
}
382+
return nil
383+
}
384+
375385
// deriveTablePathStats will fulfill the information that the accessPath need.
376386
// And it will check whether the primary key is covered only by point query.
377387
func (ds *DataSource) deriveTablePathStats(path *accessPath) (bool, error) {

planner/core/physical_plan_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ func (s *testPlanSuite) TestDAGPlanBuilderSimpleCase(c *C) {
6565
sql: "select * from t t1 use index(c_d_e)",
6666
best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))",
6767
},
68+
{
69+
sql: "select f from t use index() where f = 1",
70+
best: "TableReader(Table(t)->Sel([eq(test.t.f, 1)]))",
71+
},
6872
// Test ts + Sort vs. DoubleRead + filter.
6973
{
7074
sql: "select a from t where a between 1 and 2 order by c",

planner/core/planbuilder.go

+11
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,17 @@ func getPossibleAccessPaths(indexHints []*ast.IndexHint, tblInfo *model.TableInf
478478
}
479479

480480
hasScanHint = true
481+
482+
// It is syntactically valid to omit index_list for USE INDEX, which means “use no indexes”.
483+
// Omitting index_list for FORCE INDEX or IGNORE INDEX is a syntax error.
484+
// See https://dev.mysql.com/doc/refman/8.0/en/index-hints.html.
485+
if hint.IndexNames == nil && hint.HintType != ast.HintIgnore {
486+
if path := getTablePath(publicPaths); path != nil {
487+
hasUseOrForce = true
488+
path.forced = true
489+
available = append(available, path)
490+
}
491+
}
481492
for _, idxName := range hint.IndexNames {
482493
path := getPathByIndexName(publicPaths, idxName, tblInfo)
483494
if path == nil {

0 commit comments

Comments
 (0)