Skip to content

Commit a879ba2

Browse files
authored
planner: show un-cacheable reasons for execute statements (#40651)
ref #36598
1 parent a2e1e9b commit a879ba2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

planner/core/plan_cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func GetPlanFromSessionPlanCache(ctx context.Context, sctx sessionctx.Context,
127127
sessVars := sctx.GetSessionVars()
128128
stmtCtx := sessVars.StmtCtx
129129
stmtAst := stmt.PreparedAst
130-
stmtCtx.UseCache = stmt.StmtCacheable
130+
stmtCtx.UseCache = true
131131
if !stmt.StmtCacheable {
132132
stmtCtx.SetSkipPlanCache(errors.Errorf("skip plan-cache: %s", stmt.UncacheableReason))
133133
}

planner/core/plan_cache_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,18 @@ func TestPlanCacheWithLimit(t *testing.T) {
503503
tk.MustExec("execute stmt using @a")
504504
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip plan-cache: limit count more than 10000"))
505505
}
506+
507+
func TestUncacheableReason(t *testing.T) {
508+
store := testkit.CreateMockStore(t)
509+
tk := testkit.NewTestKit(t, store)
510+
tk.MustExec("use test")
511+
tk.MustExec("drop table if exists t")
512+
tk.MustExec("create table t(a int)")
513+
514+
tk.MustExec("prepare st from 'select /*+ ignore_plan_cache() */ * from t'")
515+
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip plan-cache: ignore plan cache by hint"))
516+
tk.MustExec("execute st") // show the un-cacheable reason when executing the statement
517+
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip plan-cache: ignore plan cache by hint"))
518+
tk.MustExec("execute st")
519+
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
520+
}

0 commit comments

Comments
 (0)