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: refactor plan cache LRU code #41618

Merged
merged 13 commits into from
Feb 22, 2023

Conversation

fzzf678
Copy link
Contributor

@fzzf678 fzzf678 commented Feb 21, 2023

What problem does this PR solve?

Issue Number: ref #40219

Problem Summary:

What is changed and how it works?

No logical change
Gather the property which can used to get plan from LRU into a struct PlanCacheMatchOpts.
We can add properties conveniently later

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Feb 21, 2023

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • Reminiscent
  • hawkingrei

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 submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot
Copy link
Member

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Feb 21, 2023
@fzzf678 fzzf678 marked this pull request as ready for review February 21, 2023 03:52
@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 Feb 21, 2023
@fzzf678 fzzf678 added sig/planner SIG: Planner epic/plan-cache needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. labels Feb 21, 2023
@fzzf678
Copy link
Contributor Author

fzzf678 commented Feb 21, 2023

/retest

util/util.go Outdated
Comment on lines 295 to 317
func (s FieldSlice) CheckTypesCompatibility4PC(tps []*types.FieldType) bool {
if len(s) != len(tps) {
return false
}
for i := range tps {
// 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
tpEqual := (s[i].GetType() == tps[i].GetType()) ||
(s[i].GetType() == mysql.TypeVarchar && tps[i].GetType() == mysql.TypeVarString) ||
(s[i].GetType() == mysql.TypeVarString && tps[i].GetType() == mysql.TypeVarchar)
if !tpEqual || s[i].GetCharset() != tps[i].GetCharset() || s[i].GetCollate() != tps[i].GetCollate() ||
(s[i].EvalType() == types.ETInt && mysql.HasUnsignedFlag(s[i].GetFlag()) != mysql.HasUnsignedFlag(tps[i].GetFlag())) {
return false
}
// When the type is decimal, we should compare the Flen and Decimal.
// We can only use the plan when both Flen and Decimal should less equal than the cached one.
// We assume here that there is no correctness problem when the precision of the parameters is less than the precision of the parameters in the cache.
if tpEqual && s[i].GetType() == mysql.TypeNewDecimal && !(s[i].GetFlen() >= tps[i].GetFlen() && s[i].GetDecimal() >= tps[i].GetDecimal()) {
return false
}
}
return true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to detach this function from FieldSlice and implement it like checktypescompativility4PC(types1, types2 []*types.FieldType) and put this new function in planner/core/plan_cache_utils.go/

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Feb 21, 2023
@fzzf678 fzzf678 removed the needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. label Feb 21, 2023
util/util.go Outdated
@@ -283,3 +284,12 @@ func ReadLines(reader *bufio.Reader, count int, maxLineSize int) ([][]byte, erro
}
return lines, nil
}

// PlanCacheMatchOpts store some property used to fetch plan from plan cache
type PlanCacheMatchOpts struct {
Copy link
Member

Choose a reason for hiding this comment

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

Can you create a plancache package in util? It can avoid unavailable cache for code import util package when updating type code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, PTAL

@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 Feb 21, 2023
@fzzf678
Copy link
Contributor Author

fzzf678 commented Feb 21, 2023

/retest

@qw4990
Copy link
Contributor

qw4990 commented Feb 22, 2023

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: be428fc

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 22, 2023
@fzzf678
Copy link
Contributor Author

fzzf678 commented Feb 22, 2023

/retest

@fzzf678
Copy link
Contributor Author

fzzf678 commented Feb 22, 2023

/merge

@fzzf678
Copy link
Contributor Author

fzzf678 commented Feb 22, 2023

/retest

@ti-chi-bot ti-chi-bot merged commit 12107e3 into pingcap:master Feb 22, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.5: #41648.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Feb 22, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
epic/plan-cache needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 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.

5 participants