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: choose TableScan when use an empty index hint #12037

Merged
merged 15 commits into from
Sep 6, 2019
10 changes: 10 additions & 0 deletions planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ type accessPath struct {
partialIndexPaths []*accessPath
}

// getTablePath finds the TablePath from a group of accessPaths.
func getTablePath(paths []*accessPath) *accessPath {
for _, path := range paths {
if path.isTablePath {
return path
}
}
return nil
}

// deriveTablePathStats will fulfill the information that the accessPath need.
// And it will check whether the primary key is covered only by point query.
func (ds *DataSource) deriveTablePathStats(path *accessPath, conds []expression.Expression) (bool, error) {
Expand Down
4 changes: 4 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (s *testPlanSuite) TestDAGPlanBuilderSimpleCase(c *C) {
sql: "select * from t t1 use index(c_d_e)",
best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))",
},
{
sql: "select f from t use index() where f = 1",
best: "TableReader(Table(t)->Sel([eq(test.t.f, 1)]))",
},
// Test ts + Sort vs. DoubleRead + filter.
{
sql: "select a from t where a between 1 and 2 order by c",
Expand Down
13 changes: 13 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,19 @@ func (b *PlanBuilder) getPossibleAccessPaths(indexHints []*ast.IndexHint, tblInf
}

hasScanHint = true
if hint.IndexNames == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also consider the comment style hint like index(t)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

// If indexNames is nil, choose TablePath.
// For example: `select a from t use index()`.
if path := getTablePath(publicPaths); path != nil {
if hint.HintType == ast.HintIgnore {
ignored = append(ignored, path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignore case doesn't make sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll remove this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is another issue. I tried ignore index() in MySQL, it returns a syntax Error.

mysql> explain select b from tt ignore index();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

But TiDB allows such syntax.

} else {
hasUseOrForce = true
path.forced = true
available = append(available, path)
}
}
}
for _, idxName := range hint.IndexNames {
path := getPathByIndexName(publicPaths, idxName, tblInfo)
if path == nil {
Expand Down