From 8e794e41cac17359faa6be4606bf4c0b8bf9dd48 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Sat, 11 Feb 2023 11:24:00 +0800 Subject: [PATCH] executor: fix unreasonable CPU time in HashJoin probe phash (#41265) (#41291) close pingcap/tidb#41263 --- executor/hash_table.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/executor/hash_table.go b/executor/hash_table.go index 57d4519be50b8..2bfdc05c69244 100644 --- a/executor/hash_table.go +++ b/executor/hash_table.go @@ -241,10 +241,14 @@ func (c *hashRowContainer) GetMatchedRowsAndPtrs(probeKey uint64, probeRow chunk matchedDataSize = int64(cap(matched))*rowSize + int64(cap(matchedPtrs))*rowPtrSize lastChunkBufPointer *chunk.Chunk = nil memDelta int64 = 0 + needTrackMemUsage = cap(innerPtrs) > signalCheckpointForJoin ) c.chkBuf = nil - c.memTracker.Consume(-c.chkBufSizeForOneProbe + int64(cap(innerPtrs))*rowPtrSize) - defer c.memTracker.Consume(-int64(cap(innerPtrs))*rowPtrSize + memDelta) + c.memTracker.Consume(-c.chkBufSizeForOneProbe) + if needTrackMemUsage { + c.memTracker.Consume(int64(cap(innerPtrs)) * rowPtrSize) + defer c.memTracker.Consume(-int64(cap(innerPtrs))*rowPtrSize + memDelta) + } c.chkBufSizeForOneProbe = 0 for i, ptr := range innerPtrs { @@ -257,13 +261,13 @@ func (c *hashRowContainer) GetMatchedRowsAndPtrs(probeKey uint64, probeRow chunk if err != nil { return nil, nil, err } - if c.chkBuf != lastChunkBufPointer && lastChunkBufPointer != nil { + if needTrackMemUsage && c.chkBuf != lastChunkBufPointer && lastChunkBufPointer != nil { lastChunkSize := lastChunkBufPointer.MemoryUsage() c.chkBufSizeForOneProbe += lastChunkSize memDelta += lastChunkSize } lastChunkBufPointer = c.chkBuf - if i&signalCheckpointForJoin == 0 { + if needTrackMemUsage && (i&signalCheckpointForJoin == (signalCheckpointForJoin - 1)) { // Trigger Consume for checking the OOM Action signal memDelta += int64(cap(matched))*rowSize + int64(cap(matchedPtrs))*rowPtrSize - matchedDataSize matchedDataSize = int64(cap(matched))*rowSize + int64(cap(matchedPtrs))*rowPtrSize