Skip to content

planner: choose TableScan when use an empty index hint#12037

Merged
sre-bot merged 15 commits into
pingcap:masterfrom
francis0407:use_index
Sep 6, 2019
Merged

planner: choose TableScan when use an empty index hint#12037
sre-bot merged 15 commits into
pingcap:masterfrom
francis0407:use_index

Conversation

@francis0407
Copy link
Copy Markdown
Member

What problem does this PR solve?

fix #12033.

schema:

mysql> create table t(a int, b int primary key, index idx_a(a));

before this pr:

mysql> explain select a from t use index();
+-------------------+----------+------+---------------------------------------------------------------------+
| id                | count    | task | operator info                                                       |
+-------------------+----------+------+---------------------------------------------------------------------+
| IndexReader_7     | 10000.00 | root | index:IndexScan_6                                                   |
| └─IndexScan_6     | 10000.00 | cop  | table:t, index:a, range:[NULL,+inf], keep order:false, stats:pseudo |
+-------------------+----------+------+---------------------------------------------------------------------+

after this pr:

mysql> explain select a from t use index();
+-------------------+----------+------+------------------------------------------------------------+
| id                | count    | task | operator info                                              |
+-------------------+----------+------+------------------------------------------------------------+
| TableReader_5     | 10000.00 | root | data:TableScan_4                                           |
| └─TableScan_4     | 10000.00 | cop  | table:t, range:[-inf,+inf], keep order:false, stats:pseudo |
+-------------------+----------+------+------------------------------------------------------------+

What is changed and how it works?

Check List

Tests

  • Unit test

@francis0407 francis0407 added type/bugfix This PR fixes a bug. sig/planner SIG: Planner labels Sep 5, 2019
@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 5, 2019

Codecov Report

Merging #12037 into master will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #12037   +/-   ##
===========================================
  Coverage   81.8853%   81.8853%           
===========================================
  Files           447        447           
  Lines         97159      97159           
===========================================
  Hits          79559      79559           
  Misses        12071      12071           
  Partials       5529       5529

Comment thread planner/core/planbuilder.go Outdated
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread planner/core/planbuilder.go Outdated
}

hasScanHint = true
if hint.IndexNames == nil {
Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed

// For example: `select a from t use index()`.
if path := getTablePath(publicPaths); path != nil {
hasUseOrForce = true
path.forced = true
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You need check whether is ignore or force.
Or you'll set table path as forced although no index is ignored by IGNORE INDEX()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed

@francis0407
Copy link
Copy Markdown
Member Author

/rebuild

@francis0407
Copy link
Copy Markdown
Member Author

/run-all-tests

@francis0407
Copy link
Copy Markdown
Member Author

/rebuild

@francis0407
Copy link
Copy Markdown
Member Author

/run-all-tests

1 similar comment
@francis0407
Copy link
Copy Markdown
Member Author

/run-all-tests

@francis0407
Copy link
Copy Markdown
Member Author

/rebuild

}

hasScanHint = true
if hint.IndexNames == nil && hint.HintType != ast.HintIgnore {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about adding a comment? From MySQL document:

It is syntactically valid to omit index_list for USE INDEX, which means “use no indexes.” Omitting index_list for FORCE INDEX or IGNORE INDEX is a syntax error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

addressed

Copy link
Copy Markdown
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

LGTM

@francis0407 francis0407 added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 6, 2019
Copy link
Copy Markdown
Contributor

@lzmhhh123 lzmhhh123 left a comment

Choose a reason for hiding this comment

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

LGTM.

@lzmhhh123 lzmhhh123 added status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 6, 2019
@sre-bot
Copy link
Copy Markdown
Contributor

sre-bot commented Sep 6, 2019

/run-all-tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sig/planner SIG: Planner status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

planner: should use TableScan when use index() has no arguments

6 participants