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: fix plan cache not working caused by type difference #23175

Merged
merged 11 commits into from
Mar 22, 2021

Conversation

time-and-fate
Copy link
Member

@time-and-fate time-and-fate commented Mar 8, 2021

What problem does this PR solve?

Issue Number: close #22952

Note: Changes in this PR assume that different Flen and Decimal of user variables won't cause a different plan.

Problem Summary:

If you set @m = 20, @n = 200.
The FieldType for m and n will be different. The flen of m will be 2 and the flen of n will be 3. Like this:
image

Because of the mechanism introduced in #18973, this will unnecessarily make this plan cache unusable.

Another problem is that the SimpleLRUCache's Put(key, value) method is only effective when it's a new key, which means you can't use Put() to update an existing key-value.

What is changed and how it works?

Only require tp, charset, and collate to be matched when verifying if a plan cache is valid.

Change (*SimpleLRUCache).Put to an override style.

Check List

Tests

  • Unit test
  • Integration test

Release note

  • no release note.

@ti-chi-bot ti-chi-bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 8, 2021
@ichn-hu ichn-hu mentioned this pull request Mar 8, 2021
@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 10, 2021
@time-and-fate time-and-fate marked this pull request as ready for review March 10, 2021 09:42
@time-and-fate time-and-fate requested review from a team as code owners March 10, 2021 09:42
@time-and-fate time-and-fate requested review from wshwsh12 and eurekaka and removed request for a team March 10, 2021 09:42
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 10, 2021
@time-and-fate
Copy link
Member Author

/sig planner

@ti-chi-bot ti-chi-bot added the sig/planner SIG: Planner label Mar 10, 2021
@time-and-fate
Copy link
Member Author

/uncc wshwsh12

@ti-chi-bot ti-chi-bot removed the request for review from wshwsh12 March 10, 2021 09:43
@time-and-fate
Copy link
Member Author

/run-all-tests

@time-and-fate
Copy link
Member Author

/run-check_dev_2

@time-and-fate
Copy link
Member Author

/run-tics-test

@time-and-fate
Copy link
Member Author

/run-unit-test

@time-and-fate
Copy link
Member Author

/run-check_dev

@time-and-fate
Copy link
Member Author

/run-check_dev_2

@time-and-fate
Copy link
Member Author

/run-all-tests

@time-and-fate
Copy link
Member Author

/run-tics-test

planner/core/common_plans.go Outdated Show resolved Hide resolved
func (s FieldSlice) Equal(tps []*types.FieldType) bool {
if len(s) != len(tps) {
return false
}
for i := range tps {
if !s[i].Equal(tps[i]) {
// We only use part of logic of `func (ft *FieldType) Equal(other *FieldType)` here because (1) only numeric and
// string types will show up here, and (2) we don't need flen and decimal to be matched exactly to use plan cache
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO, this assumption is pretty fragile. Consider this case:

MySQL [test]> create table t(a varchar(20), b int, index ia(a(4)));
Query OK, 0 rows affected (0.12 sec)

MySQL [test]> explain select * from t use index(ia) where a = 'xx';
+-------------------------------+---------+-----------+----------------------+---------------------------------------------------+
| id                            | estRows | task      | access object        | operator info                                     |
+-------------------------------+---------+-----------+----------------------+---------------------------------------------------+
| IndexLookUp_8                 | 10.00   | root      |                      |                                                   |
| ├─IndexRangeScan_5(Build)     | 10.00   | cop[tikv] | table:t, index:ia(a) | range:["xx","xx"], keep order:false, stats:pseudo |
| └─Selection_7(Probe)          | 10.00   | cop[tikv] |                      | eq(test.t.a, "xx")                                |
|   └─TableRowIDScan_6          | 10.00   | cop[tikv] | table:t              | keep order:false, stats:pseudo                    |
+-------------------------------+---------+-----------+----------------------+---------------------------------------------------+
4 rows in set (0.00 sec)

If we apply an optimization in the future to check the length of the constant string and decide if we need to keep the Selection_7, then this cached plan may return wrong results if we reuse it.

For this case, we can check curr_flen <= cached_flen to avoid potential bugs, but I am not sure if there are other possibilities that our plan may be dependent on the flen / decimal of the constants.

Copy link
Member

Choose a reason for hiding this comment

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

it's better to use another function to perform this special equal validation for plan cache.

Copy link
Member Author

Choose a reason for hiding this comment

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

After discussion, we decide to leave behind the prefix index case mentioned above for now.
Because in getPhysicalPlan, we don't know exactly how will a user variable be used. It might be inside a filter expression or part of a range.
If now we want to prevent the case, we can expect that we'll need to implement something like "find if this user variable is used on some indexes in this plan", which is complicated.
It will be easier just not to cache that plan when we implemented this optimization someday.
Also, I added some comments to clarify this.

@time-and-fate
Copy link
Member Author

/run-all-tests

@time-and-fate
Copy link
Member Author

/run-unit-test

1 similar comment
@time-and-fate
Copy link
Member Author

/run-unit-test

@time-and-fate
Copy link
Member Author

/run-all-tests

@time-and-fate
Copy link
Member Author

/run-check_dev_2

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

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

/LGTM

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 22, 2021
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • eurekaka
  • winoros

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by writing /lgtm in a comment.
Reviewer can cancel approval by writing /lgtm cancel in a comment.

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Mar 22, 2021
@winoros
Copy link
Member

winoros commented Mar 22, 2021

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 95d0556

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 22, 2021
@ti-chi-bot
Copy link
Member

@time-and-fate: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot ti-chi-bot merged commit 42be9a4 into pingcap:master Mar 22, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The value of last_plan_from_cache is not expected
5 participants