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: fix issue of explain for connection can't show the last query plan (#21242) #21315

Merged
merged 5 commits into from
Nov 26, 2020
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
22 changes: 22 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,28 @@ func (cli *testServerClient) runTestConcurrentUpdate(c *C) {
})
}

func (cli *testServerClient) runTestExplainForConn(c *C) {
cli.runTestsOnNewDB(c, nil, "explain_for_conn", func(dbt *DBTest) {
dbt.mustExec("drop table if exists t")
dbt.mustExec("create table t (a int key, b int)")
dbt.mustExec("insert t values (1, 1)")
rows := dbt.mustQuery("select connection_id();")
c.Assert(rows.Next(), IsTrue)
var connID int64
err := rows.Scan(&connID)
c.Assert(err, IsNil)
c.Assert(rows.Close(), IsNil)
dbt.mustQuery("select * from t where a=1")
rows = dbt.mustQuery("explain for connection " + strconv.Itoa(int(connID)))
c.Assert(rows.Next(), IsTrue)
row := make([]string, 9)
err = rows.Scan(&row[0], &row[1], &row[2], &row[3], &row[4], &row[5], &row[6], &row[7], &row[8])
c.Assert(err, IsNil)
c.Assert(strings.Join(row, ","), Matches, "Point_Get_1,1.00,1,root,table:t,time.*loop.*handle:1.*")
c.Assert(rows.Close(), IsNil)
})
}

func (cli *testServerClient) runTestErrorCode(c *C) {
cli.runTestsOnNewDB(c, nil, "ErrorCode", func(dbt *DBTest) {
dbt.mustExec("create table test (c int PRIMARY KEY);")
Expand Down
4 changes: 4 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func (ts *tidbTestSerialSuite) TestLoadData(c *C) {
ts.runTestLoadDataForSlowLog(c, ts.server)
}

func (ts *tidbTestSerialSuite) TestExplainFor(c *C) {
ts.runTestExplainForConn(c)
}

func (ts *tidbTestSerialSuite) TestStmtCount(c *C) {
ts.runTestStmtCount(c)
}
Expand Down
10 changes: 10 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,16 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
MaxExecutionTime: maxExecutionTime,
RedactSQL: s.sessionVars.EnableRedactLog,
}
if p == nil {
// Store the last valid plan when the current plan is nil.
// This is for `explain for connection` statement has the ability to query the last valid plan.
oldPi := s.ShowProcess()
if oldPi != nil && oldPi.Plan != nil && len(oldPi.PlanExplainRows) > 0 {
pi.Plan = oldPi.Plan
pi.PlanExplainRows = oldPi.PlanExplainRows
pi.RuntimeStatsColl = oldPi.RuntimeStatsColl
}
}
_, pi.Digest = s.sessionVars.StmtCtx.SQLDigest()
s.currentPlan = nil
if s.sessionVars.User != nil {
Expand Down