diff --git a/planner/core/prepare_test.go b/planner/core/prepare_test.go index 4b6d5b3da49dd..292c160d2a169 100644 --- a/planner/core/prepare_test.go +++ b/planner/core/prepare_test.go @@ -751,6 +751,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