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

util, executor: refine the memory usage of delete multiple tables #39489

Merged
merged 6 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
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: 12 additions & 5 deletions executor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,14 @@ func (e *DeleteExec) composeTblRowMap(tblRowMap tableRowMapType, colPosInfos []p
return err
}
// tblRowMap[info.TblID][handle] hold the row datas binding to this table and this handle.
_, exist := tblRowMap[info.TblID].Get(handle)
memDelta := tblRowMap[info.TblID].Set(handle, joinedRow[info.Start:info.End])
row, exist := tblRowMap[info.TblID].Get(handle)
if !exist {
row = make([]types.Datum, info.End-info.Start)
}
for i, d := range joinedRow[info.Start:info.End] {
d.Copy(&row[i])
}
memDelta := tblRowMap[info.TblID].Set(handle, row)
if !exist {
memDelta += types.EstimatedMemUsage(joinedRow, 1)
memDelta += int64(handle.ExtraMemSize())
Expand All @@ -192,6 +198,7 @@ func (e *DeleteExec) deleteMultiTablesByChunk(ctx context.Context) error {
fields := retTypes(e.children[0])
chk := tryNewCacheChunk(e.children[0])
memUsageOfChk := int64(0)
joinedDatumRowBuffer := make([]types.Datum, len(fields))
for {
e.memTracker.Consume(-memUsageOfChk)
iter := chunk.NewIterator4Chunk(chk)
Expand All @@ -206,13 +213,13 @@ func (e *DeleteExec) deleteMultiTablesByChunk(ctx context.Context) error {
e.memTracker.Consume(memUsageOfChk)

for joinedChunkRow := iter.Begin(); joinedChunkRow != iter.End(); joinedChunkRow = iter.Next() {
joinedDatumRow := joinedChunkRow.GetDatumRow(fields)
err := e.composeTblRowMap(tblRowMap, colPosInfos, joinedDatumRow)
joinedDatumRowBuffer = joinedChunkRow.GetDatumRowWithBuffer(fields, joinedDatumRowBuffer)
err := e.composeTblRowMap(tblRowMap, colPosInfos, joinedDatumRowBuffer)
if err != nil {
return err
}
}
chk = chunk.Renew(chk, e.maxChunkSize)
chk = tryNewCacheChunk(e.children[0])
}

return e.removeRowsInTblRowMap(tblRowMap)
Expand Down
20 changes: 15 additions & 5 deletions util/chunk/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,27 @@ func (r Row) GetJSON(colIdx int) types.BinaryJSON {
// Keep in mind that GetDatumRow has a reference to r.c, which is a chunk,
// this function works only if the underlying chunk is valid or unchanged.
func (r Row) GetDatumRow(fields []*types.FieldType) []types.Datum {
datumRow := make([]types.Datum, 0, r.c.NumCols())
for colIdx := 0; colIdx < r.c.NumCols(); colIdx++ {
datum := r.GetDatum(colIdx, fields[colIdx])
datumRow = append(datumRow, datum)
datumRow := make([]types.Datum, r.c.NumCols())
return r.GetDatumRowWithBuffer(fields, datumRow)
}

// GetDatumRowWithBuffer gets datum using the buffer datumRow.
func (r Row) GetDatumRowWithBuffer(fields []*types.FieldType, datumRow []types.Datum) []types.Datum {
for colIdx := 0; colIdx < len(datumRow); colIdx++ {
r.GetDatumWithBuffer(colIdx, fields[colIdx], &datumRow[colIdx])
}
return datumRow
}

// GetDatum implements the chunk.Row interface.
func (r Row) GetDatum(colIdx int, tp *types.FieldType) types.Datum {
var d types.Datum
r.GetDatumWithBuffer(colIdx, tp, &d)
return d
}

// GetDatumWithBuffer gets datum using the buffer d.
func (r Row) GetDatumWithBuffer(colIdx int, tp *types.FieldType, d *types.Datum) types.Datum {
switch tp.GetType() {
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
if !r.IsNull(colIdx) {
Expand Down Expand Up @@ -192,7 +202,7 @@ func (r Row) GetDatum(colIdx int, tp *types.FieldType) types.Datum {
d.SetMysqlJSON(r.GetJSON(colIdx))
}
}
return d
return *d
}

// GetRaw returns the underlying raw bytes with the colIdx.
Expand Down