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: add warning when the table name of indexHint cannot be found #15517

Merged
merged 8 commits into from
Mar 27, 2020

Conversation

francis0407
Copy link
Member

@francis0407 francis0407 commented Mar 20, 2020

What problem does this PR solve?

Problem Summary:

Throw a warning when the index hint is not applicable(i.e. cannot find the table).

What is changed and how it works?

What's Changed:
Before this PR:

mysql> explain select /*+ use_index(p, a) */ * from t;
+-----------------------+----------+-----------+-----------------------------------------+
| id                    | estRows  | task      | operator info                           |
+-----------------------+----------+-----------+-----------------------------------------+
| TableReader_5         | 10000.00 | root      | data:TableFullScan_4                    |
| └─TableFullScan_4     | 10000.00 | cop[tikv] | table:t, keep order:false, stats:pseudo |
+-----------------------+----------+-----------+-----------------------------------------+
2 rows in set (0.00 sec)

After this PR:

mysql> explain select /*+ use_index(p, a) */ * from t;
+-----------------------+----------+-----------+-----------------------------------------+
| id                    | estRows  | task      | operator info                           |
+-----------------------+----------+-----------+-----------------------------------------+
| TableReader_5         | 10000.00 | root      | data:TableFullScan_4                    |
| └─TableFullScan_4     | 10000.00 | cop[tikv] | table:t, keep order:false, stats:pseudo |
+-----------------------+----------+-----------+-----------------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1815 | IndexHint for table p is not applicable. |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)

How it Works:

Check if there are unused tableHints after building the select statement.

Check List

Tests

  • Unit test

@francis0407 francis0407 requested a review from a team as a code owner March 20, 2020 08:17
@ghost ghost requested review from eurekaka and lzmhhh123 and removed request for a team March 20, 2020 08:17
func (b *PlanBuilder) appendUnmatchedIndexHintWarning(indexHints []indexHintInfo) {
for _, hint := range indexHints {
if !hint.matched {
errMsg := fmt.Sprintf("IndexHint for table %s is not applicable.", hint.tblName)
Copy link
Member Author

Choose a reason for hiding this comment

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

Any suggestion for the warning message? cc @eurekaka @zz-jason

Copy link
Member

Choose a reason for hiding this comment

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

how about:

fmt.Sprintf(
	"%s(%d.%s, %s) is inapplicable, check whether the table(%s.%s) or index(%s) exists",
	hint.indexHint.HintType,
	hint.dbName,
	hint.tblName,
	...
)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not directly call the ast.HintTable.restore?

Copy link
Member Author

Choose a reason for hiding this comment

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

@lzmhhh123 indexHint is ast.IndexHint, it doesn't contain DB or Table when use restore.

@codecov
Copy link

codecov bot commented Mar 20, 2020

Codecov Report

Merging #15517 into master will decrease coverage by 0.3667%.
The diff coverage is 92.5000%.

@@               Coverage Diff                @@
##             master     #15517        +/-   ##
================================================
- Coverage   80.7970%   80.4303%   -0.3668%     
================================================
  Files           503        504         +1     
  Lines        136021     134550      -1471     
================================================
- Hits         109901     108219      -1682     
- Misses        17687      17842       +155     
- Partials       8433       8489        +56     

@lzmhhh123 lzmhhh123 added sig/planner SIG: Planner needs-cherry-pick-3.1 type/bugfix This PR fixes a bug. labels Mar 20, 2020
func (b *PlanBuilder) appendUnmatchedIndexHintWarning(indexHints []indexHintInfo) {
for _, hint := range indexHints {
if !hint.matched {
errMsg := fmt.Sprintf("IndexHint for table %s is not applicable.", hint.tblName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not directly call the ast.HintTable.restore?

planner/core/planbuilder.go Show resolved Hide resolved
} else {
hintTypeString = hint.hintTypeString()
}
errMsg := fmt.Sprintf("%s(%s) is inapplicable, check whether the table(%s.%s) exists",
Copy link
Member

Choose a reason for hiding this comment

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

After thinking again, how about only produce one warning for all the inapplicable index hints? It would help to reduce the warning count when a lot of hints are inapplicable.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMHO, that's not really necessary. One warning for one hint is more helpful to identify the syntax error.

Copy link
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 the status/LGT1 Indicates that a PR has LGTM 1. label Mar 24, 2020
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason added status/require-change status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Mar 26, 2020
@XuHuaiyu
Copy link
Contributor

@francis0407 Please resolve the conflicts

…int_warning

# Conflicts:
#	planner/core/testdata/plan_suite_in.json
#	planner/core/testdata/plan_suite_out.json
@francis0407
Copy link
Member Author

Conflicts are resovled. PTAL @XuHuaiyu

@XuHuaiyu
Copy link
Contributor

/merge

@sre-bot sre-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 27, 2020
@sre-bot
Copy link
Contributor

sre-bot commented Mar 27, 2020

/run-all-tests

@sre-bot sre-bot merged commit dd14172 into pingcap:master Mar 27, 2020
sre-bot pushed a commit to sre-bot/tidb that referenced this pull request Mar 27, 2020
Signed-off-by: sre-bot <sre-bot@pingcap.com>
@sre-bot
Copy link
Contributor

sre-bot commented Mar 27, 2020

cherry pick to release-3.1 in PR #15762

@sre-bot
Copy link
Contributor

sre-bot commented Mar 27, 2020

cherry pick to release-4.0 in PR #15763

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.

5 participants