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: Reduce the memory usage of InsertExec #14568

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
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
17 changes: 11 additions & 6 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/memory"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -214,7 +215,9 @@ func insertRows(ctx context.Context, base insertCommon) (err error) {
evalRowFunc = e.evalRow
}

rows := make([][]types.Datum, 0, len(e.Lists))
rowLen := mathutil.Min(len(e.Lists), batchSize)
rows := make([][]types.Datum, 0, rowLen)

memUsageOfRows := int64(0)
memTracker := e.memTracker
for i, list := range e.Lists {
Expand All @@ -225,8 +228,8 @@ func insertRows(ctx context.Context, base insertCommon) (err error) {
return err
}
rows = append(rows, row)
if batchInsert && e.rowCount%uint64(batchSize) == 0 {
memUsageOfRows = types.EstimatedMemUsage(rows[0], len(rows))
if e.rowCount%uint64(batchSize) == 0 {
memUsageOfRows = types.EstimatedMemUsage(rows[0], cap(rows))
memTracker.Consume(memUsageOfRows)
// Before batch insert, fill the batch allocated autoIDs.
rows, err = e.lazyAdjustAutoIncrementDatum(ctx, rows)
Expand All @@ -239,13 +242,15 @@ func insertRows(ctx context.Context, base insertCommon) (err error) {
rows = rows[:0]
memTracker.Consume(-memUsageOfRows)
memUsageOfRows = 0
if err = e.doBatchInsert(ctx); err != nil {
return err
if batchInsert {
if err = e.doBatchInsert(ctx); err != nil {
return err
}
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
if len(rows) != 0 {
memUsageOfRows = types.EstimatedMemUsage(rows[0], len(rows))
memUsageOfRows = types.EstimatedMemUsage(rows[0], cap(rows))
memTracker.Consume(memUsageOfRows)
}
// Fill the batch allocated autoIDs.
Expand Down