From ffaf2ac9eeec01d0089bc5106dfa63f6dbe8ee8f Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Fri, 30 Dec 2022 10:20:17 +0800 Subject: [PATCH] planner: remove the unnecessary skip-plan-cache flag in StmtCtx (#40235) --- expression/builtin_compare.go | 2 +- expression/util.go | 2 +- planner/core/expression_rewriter.go | 2 +- planner/core/plan_cache.go | 4 ++-- sessionctx/stmtctx/stmtctx.go | 5 ++--- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index 4ffd0a3dbe6be..dec5d06983679 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -1586,7 +1586,7 @@ func (c *compareFunctionClass) refineArgs(ctx sessionctx.Context, args []Express } else { return args } - } else if ctx.GetSessionVars().StmtCtx.SkipPlanCache { + } else if !ctx.GetSessionVars().StmtCtx.UseCache { // We should remove the mutable constant for correctness, because its value may be changed. RemoveMutableConst(ctx, args) } diff --git a/expression/util.go b/expression/util.go index e19ec047e43d0..0de4253aec5f9 100644 --- a/expression/util.go +++ b/expression/util.go @@ -1241,7 +1241,7 @@ func ContainCorrelatedColumn(exprs []Expression) bool { // TODO: Do more careful check here. func MaybeOverOptimized4PlanCache(ctx sessionctx.Context, exprs []Expression) bool { // If we do not enable plan cache, all the optimization can work correctly. - if !ctx.GetSessionVars().StmtCtx.UseCache || ctx.GetSessionVars().StmtCtx.SkipPlanCache { + if !ctx.GetSessionVars().StmtCtx.UseCache { return false } return containMutableConst(ctx, exprs) diff --git a/planner/core/expression_rewriter.go b/planner/core/expression_rewriter.go index 2694d81ef0a65..5a8f8a2df9f35 100644 --- a/planner/core/expression_rewriter.go +++ b/planner/core/expression_rewriter.go @@ -1567,7 +1567,7 @@ func (er *expressionRewriter) inToExpression(lLen int, not bool, tp *types.Field } else { continue } - } else if er.sctx.GetSessionVars().StmtCtx.SkipPlanCache { + } else if !er.sctx.GetSessionVars().StmtCtx.UseCache { // We should remove the mutable constant for correctness, because its value may be changed. expression.RemoveMutableConst(er.sctx, []expression.Expression{c}) } diff --git a/planner/core/plan_cache.go b/planner/core/plan_cache.go index 9739a63f2b8ff..8036f4067ce65 100644 --- a/planner/core/plan_cache.go +++ b/planner/core/plan_cache.go @@ -280,7 +280,7 @@ func generateNewPlan(ctx context.Context, sctx sessionctx.Context, isNonPrepared if containTableDual(p) && paramNum > 0 { stmtCtx.SetSkipPlanCache(errors.New("skip plan-cache: get a TableDual plan")) } - if stmtAst.UseCache && !stmtCtx.SkipPlanCache && !ignorePlanCache { + if stmtCtx.UseCache && !ignorePlanCache { // rebuild key to exclude kv.TiFlash when stmt is not read only if _, isolationReadContainTiFlash := sessVars.IsolationReadEngines[kv.TiFlash]; isolationReadContainTiFlash && !IsReadOnly(stmtAst.Stmt, sessVars) { delete(sessVars.IsolationReadEngines, kv.TiFlash) @@ -640,7 +640,7 @@ func CheckPreparedPriv(sctx sessionctx.Context, stmt *PlanCacheStmt, is infosche // short paths for these executions, currently "point select" and "point update" func tryCachePointPlan(_ context.Context, sctx sessionctx.Context, stmt *PlanCacheStmt, _ infoschema.InfoSchema, p Plan) error { - if !sctx.GetSessionVars().StmtCtx.UseCache || sctx.GetSessionVars().StmtCtx.SkipPlanCache { + if !sctx.GetSessionVars().StmtCtx.UseCache { return nil } var ( diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index cfe017c7c8805..01ead10e580fc 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -172,7 +172,6 @@ type StatementContext struct { InNullRejectCheck bool AllowInvalidDate bool IgnoreNoPartition bool - SkipPlanCache bool IgnoreExplainIDSuffix bool SkipUTF8Check bool SkipASCIICheck bool @@ -611,10 +610,10 @@ func (sc *StatementContext) SetPlanHint(hint string) { // SetSkipPlanCache sets to skip the plan cache and records the reason. func (sc *StatementContext) SetSkipPlanCache(reason error) { - if sc.UseCache && sc.SkipPlanCache { + if !sc.UseCache { return // avoid unnecessary warnings } - sc.SkipPlanCache = true + sc.UseCache = false sc.AppendWarning(reason) }