From 36b55a35297c0d91970a24bda04c88a8ee6174e8 Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Fri, 8 Jan 2021 14:03:10 +0800 Subject: [PATCH] cherry pick #22219 to release-4.0 Signed-off-by: ti-srebot --- bindinfo/bind_test.go | 18 ++++++++++++++++++ executor/adapter.go | 11 +++++++---- sessionctx/stmtctx/stmtctx.go | 6 ++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bindinfo/bind_test.go b/bindinfo/bind_test.go index d38cbbae8bd81..48be771c15e7f 100644 --- a/bindinfo/bind_test.go +++ b/bindinfo/bind_test.go @@ -1802,3 +1802,21 @@ func (s *testSuite) TestCapturedBindingCharset(c *C) { c.Assert(rows[0][6], Equals, "") c.Assert(rows[0][7], Equals, "") } + +func (s *testSuite) TestCaptureWithZeroSlowLogThreshold(c *C) { + tk := testkit.NewTestKit(c, s.store) + s.cleanBindingEnv(tk) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int)") + stmtsummary.StmtSummaryByDigestMap.Clear() + c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue) + tk.MustExec("set tidb_slow_log_threshold = 0") + tk.MustExec("select * from t") + tk.MustExec("select * from t") + tk.MustExec("set tidb_slow_log_threshold = 300") + tk.MustExec("admin capture bindings") + rows := tk.MustQuery("show global bindings").Rows() + c.Assert(len(rows), Equals, 1) + c.Assert(rows[0][0], Equals, "select * from test . t") +} diff --git a/executor/adapter.go b/executor/adapter.go index ce9d68de32ab4..4c1c2a2343194 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -966,13 +966,16 @@ func getPlanDigest(sctx sessionctx.Context, p plannercore.Plan) (normalized, pla // getEncodedPlan gets the encoded plan, and generates the hint string if indicated. func getEncodedPlan(sctx sessionctx.Context, p plannercore.Plan, genHint bool, n ast.StmtNode) (encodedPlan, hintStr string) { + var hintSet bool encodedPlan = sctx.GetSessionVars().StmtCtx.GetEncodedPlan() - hintStr = sctx.GetSessionVars().StmtCtx.GetPlanHint() - if len(encodedPlan) > 0 { + hintStr, hintSet = sctx.GetSessionVars().StmtCtx.GetPlanHint() + if len(encodedPlan) > 0 && (!genHint || hintSet) { return } - encodedPlan = plannercore.EncodePlan(p) - sctx.GetSessionVars().StmtCtx.SetEncodedPlan(encodedPlan) + if len(encodedPlan) == 0 { + encodedPlan = plannercore.EncodePlan(p) + sctx.GetSessionVars().StmtCtx.SetEncodedPlan(encodedPlan) + } if genHint { hints := plannercore.GenHintsFromPhysicalPlan(p) if n != nil { diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index d0f461e814ac9..aaf8b562f379f 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -147,6 +147,7 @@ type StatementContext struct { planDigest string encodedPlan string planHint string + planHintSet bool Tables []TableEntry PointExec bool // for point update cached execution, Constant expression need to set "paramMarker" lockWaitStartTime int64 // LockWaitStartTime stores the pessimistic lock wait start time @@ -229,12 +230,13 @@ func (sc *StatementContext) SetEncodedPlan(encodedPlan string) { } // GetPlanHint gets the hint string generated from the plan. -func (sc *StatementContext) GetPlanHint() string { - return sc.planHint +func (sc *StatementContext) GetPlanHint() (string, bool) { + return sc.planHint, sc.planHintSet } // SetPlanHint sets the hint for the plan. func (sc *StatementContext) SetPlanHint(hint string) { + sc.planHintSet = true sc.planHint = hint }