diff --git a/planner/core/prepare_test.go b/planner/core/prepare_test.go index 9630225d1c7e2..993c091159617 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -763,6 +763,43 @@ func (s *testPlanSerialSuite) TestPlanCacheUnsignedHandleOverflow(c *C) { tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) } +func (s *testPlanSerialSuite) TestIssue18066(c *C) { + defer testleak.AfterTest(c)() + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + tk := testkit.NewTestKit(c, store) + orgEnable := core.PreparedPlanCacheEnabled() + defer func() { + dom.Close() + store.Close() + core.SetPreparedPlanCache(orgEnable) + }() + core.SetPreparedPlanCache(true) + + tk.Se, err = session.CreateSession4TestWithOpt(store, &session.Opt{ + PreparedPlanCache: kvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64), + }) + c.Assert(err, IsNil) + tk.GetConnectionID() + c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int)") + tk.MustExec("prepare stmt from 'select * from t'") + tk.MustQuery("execute stmt").Check(testkit.Rows()) + tk.MustQuery("select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from t'").Check( + testkit.Rows("1 0 0")) + tk.MustQuery("execute stmt").Check(testkit.Rows()) + tk.MustQuery("select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from t'").Check( + testkit.Rows("2 1 1")) + tk.MustExec("prepare stmt from 'select * from t'") + tk.MustQuery("execute stmt").Check(testkit.Rows()) + tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) + tk.MustQuery("select EXEC_COUNT,plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from t'").Check( + testkit.Rows("3 1 0")) +} + func (s *testPrepareSuite) TestPrepareForGroupByMultiItems(c *C) { defer testleak.AfterTest(c)() store, dom, err := newStoreWithBootstrap() diff --git a/util/stmtsummary/statement_summary.go b/util/stmtsummary/statement_summary.go index 98f07ca3becd1..bab199e994a05 100644 --- a/util/stmtsummary/statement_summary.go +++ b/util/stmtsummary/statement_summary.go @@ -731,6 +731,8 @@ func (ssElement *stmtSummaryByDigestElement) add(sei *StmtExecInfo, intervalSeco if sei.PlanInCache { ssElement.planInCache = true ssElement.planCacheHits += 1 + } else { + ssElement.planInCache = false } // other