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 the issue that some PointGet plans generated in physical-stage cannot be cached #28185

Closed
wants to merge 12 commits into from

Conversation

qw4990
Copy link
Contributor

@qw4990 qw4990 commented Sep 17, 2021

What problem does this PR solve?

Issue Number: close #26868

Problem Summary: planner: fix the issue that some PointGet plans generated in physical-stage cannot be cached

What is changed and how it works?

planner: fix the issue that some PointGet plans generated in physical-stage cannot be cached

Check List

Tests

  • Unit test

Release note

planner: fix the issue that some PointGet plans generated in physical-stage cannot be cached

@qw4990 qw4990 added type/enhancement The issue or PR belongs to an enhancement. sig/planner SIG: Planner labels Sep 17, 2021
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

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 ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 17, 2021
@@ -789,8 +789,8 @@ func SplitDNFItems(onExpr Expression) []Expression {
// EvaluateExprWithNull sets columns in schema as null and calculate the final result of the scalar function.
// If the Expression is a non-constant value, it means the result is unknown.
func EvaluateExprWithNull(ctx sessionctx.Context, schema *Schema, expr Expression) Expression {
if ContainMutableConst(ctx, []Expression{expr}) {
ctx.GetSessionVars().StmtCtx.OptimDependOnMutableConst = true
if ctx.GetSessionVars().StmtCtx.UseCache && ContainMutableConst(ctx, []Expression{expr}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The function ContainMutableConst contains the check for ctx.GetSessionVars().StmtCtx.UseCache.

@@ -1107,7 +1149,7 @@ func (s *testPlanSerialSuite) TestPlanCachePointGetAndTableDual(c *C) {
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
// Must not reuse the previous TableDual plan.
Copy link
Contributor

@Reminiscent Reminiscent Sep 21, 2021

Choose a reason for hiding this comment

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

Please modify the comment here. Besides, we need to check the plan from the cache. The following changes are the same.

@@ -604,7 +602,7 @@ func (ds *DataSource) isInIndexMergeHints(name string) bool {
}

// accessPathsForConds generates all possible index paths for conditions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update the comment here.

Comment on lines -346 to -347
// TODO: Can we make a more careful check on whether the optimization depends on mutable constants?
ds.ctx.GetSessionVars().StmtCtx.OptimDependOnMutableConst = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there some test cases that can cover this change?

@@ -566,7 +566,9 @@ func ExtractEqAndInCondition(sctx sessionctx.Context, conditions []expression.Ex
// Maybe we can improve it later.
columnValues[i] = &valueInfo{mutable: true}
}
sctx.GetSessionVars().StmtCtx.OptimDependOnMutableConst = true
if sctx.GetSessionVars().StmtCtx.UseCache {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the prepare stmt like prepare stmt for 'select * from t where a > 0 and a < 2' which there are no parameters, we should use the PointGet plan. But here we can't use this plan?

Copy link
Contributor

Choose a reason for hiding this comment

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

In real workloads there may be a small chance to convert a > ? and a < ? to equal condition, so maybe it is acceptable to give up the optimization when prepare-plan-cache is enabled.

@ti-chi-bot
Copy link
Member

@qw4990: PR needs rebase.

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 kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 26, 2021
canConvertPointGet := len(path.Ranges) > 0 && path.StoreType == kv.TiKV && ds.isPointGetConvertableSchema() &&
// to avoid the over-optimized risk, do not generate PointGet for plan cache, for example,
// `pk>=$a and pk<=$b` can be optimized to a PointGet when `$a==$b`, but it can cause wrong results when `$a!=$b`.
!ds.ctx.GetSessionVars().StmtCtx.UseCache
Copy link
Contributor

Choose a reason for hiding this comment

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

Here we cannot generate PointGet if prepared-cache-plan is enabled. Will that be a problem in OLTP workload? For example, as for prepare stmt1 from "insert into t2 select * from t1 where a=?", if the sql will be executed frequently, we may hope to not only generate PointGet plan but also cache the plan.

@qw4990
Copy link
Contributor Author

qw4990 commented Sep 29, 2021

There are too many conflicts, I'll close this PR and submit a new one later.

@qw4990 qw4990 closed this Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prepared statement doesn't work on 'insert into t1 select from t2 where id = ?'
4 participants