Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: store correct plan hint in statements_summary when log level is 'debug' (#22219) #22293

Merged
merged 1 commit into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
11 changes: 7 additions & 4 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down