diff --git a/executor/batch_point_get.go b/executor/batch_point_get.go index d8f1640687173..dc6d7324983ec 100644 --- a/executor/batch_point_get.go +++ b/executor/batch_point_get.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/parser/model" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/store/tikv" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/types" @@ -59,6 +60,9 @@ type BatchPointGetExec struct { // virtualColumnRetFieldTypes records the RetFieldTypes of virtual columns. virtualColumnRetFieldTypes []*types.FieldType + + snapshot kv.Snapshot + stats *pointGetRuntimeStats } // buildVirtualColumnInfo saves virtual column indices and sort them in definition order @@ -79,6 +83,9 @@ func (e *BatchPointGetExec) Open(context.Context) error { // Close implements the Executor interface. func (e *BatchPointGetExec) Close() error { + if e.runtimeStats != nil && e.snapshot != nil { + e.snapshot.DelOption(kv.CollectRuntimeStats) + } return nil } @@ -133,6 +140,16 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error { return err } } + if e.runtimeStats != nil { + snapshotStats := &tikv.SnapshotRuntimeStats{} + e.stats = &pointGetRuntimeStats{ + BasicRuntimeStats: e.runtimeStats, + SnapshotRuntimeStats: snapshotStats, + } + snapshot.SetOption(kv.CollectRuntimeStats, snapshotStats) + e.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(e.id.String(), e.stats) + e.snapshot = snapshot + } if e.ctx.GetSessionVars().GetReplicaRead().IsFollowerRead() { snapshot.SetOption(kv.ReplicaRead, kv.ReplicaReadFollower) } diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 3cccacc820ea3..64357265458e2 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -1188,11 +1188,16 @@ func (s *testIntegrationSerialSuite) TestExplainAnalyzePointGet(c *C) { tk.MustExec("insert into t values (1,1)") res := tk.MustQuery("explain analyze select * from t where a=1;") - resBuff := bytes.NewBufferString("") - for _, row := range res.Rows() { - fmt.Fprintf(resBuff, "%s\n", row) + 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)) } - explain := resBuff.String() - c.Assert(strings.Contains(explain, "Get:{num_rpc:"), IsTrue, Commentf("%s", explain)) - c.Assert(strings.Contains(explain, "total_time:"), IsTrue, Commentf("%s", explain)) + checkExplain("Get") + res = tk.MustQuery("explain analyze select * from t where a in (1,2,3);") + checkExplain("BatchGet") }