diff --git a/executor/executor.go b/executor/executor.go index b825908a0d29a..9474efaaddde6 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1029,6 +1029,8 @@ type SelectionExec struct { inputIter *chunk.Iterator4Chunk inputRow chunk.Row childResult *chunk.Chunk + + memTracker *memory.Tracker } // Open implements the Executor Open interface. @@ -1036,7 +1038,10 @@ func (e *SelectionExec) Open(ctx context.Context) error { if err := e.baseExecutor.Open(ctx); err != nil { return err } + e.memTracker = memory.NewTracker(e.id, -1) + e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker) e.childResult = newFirstChunk(e.children[0]) + e.memTracker.Consume(e.childResult.MemoryUsage()) e.batched = expression.Vectorizable(e.filters) if e.batched { e.selected = make([]bool, 0, chunk.InitialCapacity) @@ -1048,6 +1053,7 @@ func (e *SelectionExec) Open(ctx context.Context) error { // Close implements plannercore.Plan Close interface. func (e *SelectionExec) Close() error { + e.memTracker.Consume(-e.childResult.MemoryUsage()) e.childResult = nil e.selected = nil return e.baseExecutor.Close() @@ -1071,7 +1077,9 @@ func (e *SelectionExec) Next(ctx context.Context, req *chunk.Chunk) error { } req.AppendRow(e.inputRow) } + mSize := e.childResult.MemoryUsage() err := Next(ctx, e.children[0], e.childResult) + e.memTracker.Consume(e.childResult.MemoryUsage() - mSize) if err != nil { return err } @@ -1103,7 +1111,9 @@ func (e *SelectionExec) unBatchedNext(ctx context.Context, chk *chunk.Chunk) err return nil } } + mSize := e.childResult.MemoryUsage() err := Next(ctx, e.children[0], e.childResult) + e.memTracker.Consume(e.childResult.MemoryUsage() - mSize) if err != nil { return err } diff --git a/executor/explain_test.go b/executor/explain_test.go index 8af54865216c0..3af5cc2321687 100644 --- a/executor/explain_test.go +++ b/executor/explain_test.go @@ -129,7 +129,7 @@ func (s *testSuite1) TestExplainAnalyzeMemory(c *C) { func (s *testSuite1) checkMemoryInfo(c *C, tk *testkit.TestKit, sql string) { memCol := 5 - ops := []string{"Join", "Reader", "Top", "Sort", "LookUp", "Projection"} + ops := []string{"Join", "Reader", "Top", "Sort", "LookUp", "Projection", "Selection"} rows := tk.MustQuery(sql).Rows() for _, row := range rows { strs := make([]string, len(row)) @@ -164,6 +164,7 @@ func (s *testSuite1) TestMemoryUsageAfterClose(c *C) { tk.MustExec(fmt.Sprintf("insert into t values (%v, %v)", i, i)) } SQLs := []string{"select v+abs(k) from t", + "select v from t where abs(v) > 0", "select v from t order by v"} for _, sql := range SQLs { tk.MustQuery(sql)