From 155c82ee0bd74dcc1f31012df9afa5476c00b369 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Sun, 28 Nov 2021 11:26:36 +0800 Subject: [PATCH] executor: track the memory usage of IndexMergeReaderExecutor Signed-off-by: guo-shaoge --- executor/index_merge_reader.go | 4 +++- executor/index_merge_reader_test.go | 31 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/executor/index_merge_reader.go b/executor/index_merge_reader.go index 60828bd514ac4..cd27a5049b8b5 100644 --- a/executor/index_merge_reader.go +++ b/executor/index_merge_reader.go @@ -133,6 +133,8 @@ func (e *IndexMergeReaderExecutor) Open(ctx context.Context) (err error) { } e.finished = make(chan struct{}) e.resultCh = make(chan *lookupTableTask, atomic.LoadInt32(&LookupTableTaskChannelSize)) + e.memTracker = memory.NewTracker(e.id, -1) + e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker) return nil } @@ -520,7 +522,7 @@ func (e *IndexMergeReaderExecutor) startIndexMergeTableScanWorker(ctx context.Co finished: e.finished, indexMergeExec: e, tblPlans: e.tblPlans, - memTracker: memory.NewTracker(memory.LabelForSimpleTask, -1), + memTracker: e.memTracker, } ctx1, cancel := context.WithCancel(ctx) go func() { diff --git a/executor/index_merge_reader_test.go b/executor/index_merge_reader_test.go index 51620b861a94a..2c3c6083fadae 100644 --- a/executor/index_merge_reader_test.go +++ b/executor/index_merge_reader_test.go @@ -170,3 +170,34 @@ func (s *testSuite1) TestPartitionTableRandomIndexMerge(c *C) { tk.MustQuery("select /*+ USE_INDEX_MERGE(tpk, a, b) */ * from tpk where " + cond).Sort().Check(result) } } + +func (test *testSerialSuite2) TestIndexMergeReaderMemTracker(c *C) { + tk := testkit.NewTestKit(c, test.store) + tk.MustExec("use test;") + tk.MustExec("create table t1(c1 int, c2 int, c3 int, key(c1), key(c2), key(c3));") + + insertStr := "insert into t1 values(0, 0, 0)" + rowNum := 1000 + for i := 0; i < rowNum; i++ { + insertStr += fmt.Sprintf(" ,(%d, %d, %d)", i, i, i) + } + insertStr += ";" + memTracker := tk.Se.GetSessionVars().StmtCtx.MemTracker + + tk.MustExec(insertStr) + + oriMaxUsage := memTracker.MaxConsumed() + + // We select all rows in t1, so the mem usage is more clear. + tk.MustQuery("select /*+ use_index_merge(t1) */ * from t1 where c1 > 1 or c2 > 1") + + newMaxUsage := memTracker.MaxConsumed() + c.Assert(newMaxUsage, Greater, oriMaxUsage) + + // We expect memory is not N/A. + res := tk.MustQuery("explain analyze select /*+ use_index_merge(t1) */ * from t1 where c1 > 1 or c2 > 1") + c.Assert(len(res.Rows()), Equals, 4) + if res.Rows()[1][6] == "N/A" { + c.Assert(1, Equals, 0) + } +}