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: add runtime information for DML statement in explain analyze #19106

Merged
merged 29 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
248b3eb
Improvement on explaininfo of MetricSummaryTableExtractor #15384
jianyilyu Jul 29, 2020
1b4ac86
Improvement on explaininfo of MetricSummaryTableExtractor #15384
jianyilyu Jul 30, 2020
9a96da6
Merge branch 'master' into master
jianyilyu Jul 30, 2020
5e74b33
Fix up displaying executor runtime info & Add runtime information for…
jianyilyu Aug 10, 2020
ca772ae
Merge branch 'master' into #18056
crazycs520 Aug 10, 2020
05cd8db
Fix up displaying executor runtime info & Add runtime information for…
jianyilyu Aug 10, 2020
63dffd1
Merge remote-tracking branch 'origin/#18056' into #18056
jianyilyu Aug 10, 2020
34094de
a few updates
jianyilyu Aug 11, 2020
f81f634
remove irrelevant go files #18056
jianyilyu Aug 11, 2020
a5817da
remove several useless lines #18056
jianyilyu Aug 11, 2020
c5bfb7d
Some format changes #18056
jianyilyu Aug 12, 2020
b6c26b7
Merge branch 'master' into #18056
crazycs520 Aug 12, 2020
e753d44
Some format changes #18056
jianyilyu Aug 12, 2020
dc3d9fc
Merge remote-tracking branch 'origin/#18056' into #18056
jianyilyu Aug 12, 2020
f7b4d8c
Some changes #18056
jianyilyu Aug 12, 2020
7f6d2ad
Merge branch 'master' into #18056
qw4990 Aug 19, 2020
d262828
Merge branch 'master' of https://github.com/pingcap/tidb into #18056
crazycs520 Sep 3, 2020
14a13e8
Merge branch 'master' of https://github.com/pingcap/tidb into #18056
crazycs520 Sep 18, 2020
5f595c3
init
crazycs520 Sep 18, 2020
6dc89ff
Merge branch 'master' of https://github.com/pingcap/tidb into explain…
crazycs520 Sep 21, 2020
c7520ff
fix it
crazycs520 Sep 21, 2020
8867e2b
fix bug
crazycs520 Sep 21, 2020
392ec9f
refine code
crazycs520 Sep 21, 2020
f2150b4
Merge branch 'master' into #18056
crazycs520 Sep 21, 2020
b414c86
Merge branch 'master' into #18056
crazycs520 Sep 22, 2020
fb3c6c7
add comment
crazycs520 Sep 22, 2020
f2a0b2b
Merge branch 'master' into #18056
crazycs520 Sep 25, 2020
c1f9e6c
Merge branch 'master' into #18056
crazycs520 Sep 27, 2020
0aad9ac
Merge branch 'master' into #18056
crazycs520 Oct 10, 2020
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
13 changes: 8 additions & 5 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,22 @@ func (a *ExecStmt) handleNoDelay(ctx context.Context, e Executor, isPessimistic
}()

toCheck := e
isExplainAnalyze := false
if explain, ok := e.(*ExplainExec); ok {
if explain.analyzeExec != nil {
toCheck = explain.analyzeExec
if analyze := explain.getAnalyzeExecToExecutedNoDelay(); analyze != nil {
toCheck = analyze
isExplainAnalyze = true
}
}

// If the executor doesn't return any result to the client, we execute it without delay.
if toCheck.Schema().Len() == 0 {
handled = !isExplainAnalyze
if isPessimistic {
return true, nil, a.handlePessimisticDML(ctx, e)
return handled, nil, a.handlePessimisticDML(ctx, toCheck)
}
r, err := a.handleNoDelayExecutor(ctx, e)
return true, r, err
r, err := a.handleNoDelayExecutor(ctx, toCheck)
return handled, r, err
} else if proj, ok := toCheck.(*ProjectionExec); ok && proj.calculateNoDelay {
// Currently this is only for the "DO" statement. Take "DO 1, @a=2;" as an example:
// the Projection has two expressions and two columns in the schema, but we should
Expand Down
17 changes: 16 additions & 1 deletion executor/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ExplainExec struct {

explain *core.Explain
analyzeExec Executor
executed bool
rows [][]string
cursor int
}
Expand Down Expand Up @@ -79,7 +80,8 @@ func (e *ExplainExec) generateExplainInfo(ctx context.Context) (rows [][]string,
closed = true
}
}()
if e.analyzeExec != nil {
if e.analyzeExec != nil && !e.executed {
e.executed = true
chk := newFirstChunk(e.analyzeExec)
var nextErr, closeErr error
for {
Expand Down Expand Up @@ -111,3 +113,16 @@ func (e *ExplainExec) generateExplainInfo(ctx context.Context) (rows [][]string,
}
return e.explain.Rows, nil
}

// getAnalyzeExecToExecutedNoDelay gets the analyze DML executor to execute in handleNoDelay function.
// For explain analyze insert/update/delete statement, the analyze executor should be executed in handleNoDelay
// function and then commit transaction if needed.
// Otherwise, in autocommit transaction, the table record change of analyze executor(insert/update/delete...)
// will not be committed.
func (e *ExplainExec) getAnalyzeExecToExecutedNoDelay() Executor {
if e.analyzeExec != nil && !e.executed && e.analyzeExec.Schema().Len() == 0 {
e.executed = true
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
return e.analyzeExec
}
return nil
}
22 changes: 22 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,28 @@ func (s *testIntegrationSerialSuite) TestExplainAnalyzePointGet(c *C) {
checkExplain("BatchGet")
}

func (s *testIntegrationSerialSuite) TestExplainAnalyzeDML(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec(" create table t (a int, b int, unique index (a));")
tk.MustExec("insert into t values (1,1)")

res := tk.MustQuery("explain analyze select * from t where a=1;")
checkExplain := func(rpc string) {
resBuff := bytes.NewBufferString("")
for _, row := range res.Rows() {
fmt.Fprintf(resBuff, "%s\n", row)
}
explain := resBuff.String()
c.Assert(strings.Contains(explain, rpc+":{num_rpc:"), IsTrue, Commentf("%s", explain))
c.Assert(strings.Contains(explain, "total_time:"), IsTrue, Commentf("%s", explain))
}
checkExplain("Get")
res = tk.MustQuery("explain analyze insert ignore into t values (1,1),(2,2),(3,3),(4,4);")
checkExplain("BatchGet")
}

func (s *testIntegrationSuite) TestPartitionExplain(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
3 changes: 1 addition & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ func runStmt(ctx context.Context, se *session, s sqlexec.Statement) (rs sqlexec.
se.StmtCommit()
}
}
err = finishStmt(ctx, se, err, s)
}
if rs != nil {
return &execStmtResult{
Expand All @@ -1234,8 +1235,6 @@ func runStmt(ctx context.Context, se *session, s sqlexec.Statement) (rs sqlexec.
}, err
}

err = finishStmt(ctx, se, err, s)

// If it is not a select statement, we record its slow log here,
// then it could include the transaction commit time.
s.(*executor.ExecStmt).FinishExecuteStmt(origTxnCtx.StartTS, err == nil, false)
Expand Down