Skip to content

Commit 8483dc5

Browse files
authored
table: Update delta from column slice, instead of map (#53670)
close #53660
1 parent 4670bf5 commit 8483dc5

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

pkg/sessionctx/variable/session.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,35 @@ func (tc *TransactionContext) UpdateDeltaForTable(physicalTableID int64, delta i
325325
tc.TableDeltaMap[physicalTableID] = item
326326
}
327327

328+
// ColSize is a data struct to store the delta information for a table.
329+
type ColSize struct {
330+
ColID int64
331+
Size int64
332+
}
333+
334+
// UpdateDeltaForTableFromColSlice is the same as UpdateDeltaForTable, but it accepts a slice of column size.
335+
func (tc *TransactionContext) UpdateDeltaForTableFromColSlice(
336+
physicalTableID int64, delta int64,
337+
count int64, colSizes []ColSize,
338+
) {
339+
tc.tdmLock.Lock()
340+
defer tc.tdmLock.Unlock()
341+
if tc.TableDeltaMap == nil {
342+
tc.TableDeltaMap = make(map[int64]TableDelta)
343+
}
344+
item := tc.TableDeltaMap[physicalTableID]
345+
if item.ColSize == nil && len(colSizes) > 0 {
346+
item.ColSize = make(map[int64]int64, len(colSizes))
347+
}
348+
item.Delta += delta
349+
item.Count += count
350+
item.TableID = physicalTableID
351+
for _, s := range colSizes {
352+
item.ColSize[s.ColID] += s.Size
353+
}
354+
tc.TableDeltaMap[physicalTableID] = item
355+
}
356+
328357
// GetKeyInPessimisticLockCache gets a key in pessimistic lock cache.
329358
func (tc *TransactionContext) GetKeyInPessimisticLockCache(key kv.Key) (val []byte, ok bool) {
330359
if tc.pessimisticLockCache == nil && tc.CurrentStmtPessimisticLockCache == nil {

pkg/table/tables/bench_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import (
2929
func BenchmarkAddRecordInPipelinedDML(b *testing.B) {
3030
store, dom := testkit.CreateMockStoreAndDomain(b)
3131
tk := testkit.NewTestKit(b, store)
32-
_, err := tk.Session().Execute(context.Background(), "CREATE TABLE test.t (a int primary key auto_increment, b varchar(255))")
32+
_, err := tk.Session().Execute(
33+
context.Background(),
34+
"CREATE TABLE test.t (a int primary key auto_increment, b varchar(255))",
35+
)
3336
require.NoError(b, err)
3437
ctx := tk.Session()
3538
vars := ctx.GetSessionVars()

pkg/table/tables/tables.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ func (t *TableCommon) UpdateRecord(ctx context.Context, sctx table.MutateContext
671671
return err
672672
}
673673
}
674-
colSize := make(map[int64]int64, len(t.Cols()))
674+
colSize := make([]variable.ColSize, len(t.Cols()))
675675
for id, col := range t.Cols() {
676676
size, err := codec.EstimateValueSize(sc.TypeCtx(), newData[id])
677677
if err != nil {
@@ -683,9 +683,9 @@ func (t *TableCommon) UpdateRecord(ctx context.Context, sctx table.MutateContext
683683
continue
684684
}
685685
oldLen := size - 1
686-
colSize[col.ID] = int64(newLen - oldLen)
686+
colSize[id] = variable.ColSize{ColID: col.ID, Size: int64(newLen - oldLen)}
687687
}
688-
sessVars.TxnCtx.UpdateDeltaForTable(t.physicalTableID, 0, 1, colSize)
688+
sessVars.TxnCtx.UpdateDeltaForTableFromColSlice(t.physicalTableID, 0, 1, colSize)
689689
return nil
690690
}
691691

@@ -1171,15 +1171,15 @@ func (t *TableCommon) AddRecord(sctx table.MutateContext, r []types.Datum, opts
11711171
sessVars.TxnCtx.InsertTTLRowsCount += 1
11721172
}
11731173

1174-
colSize := make(map[int64]int64, len(r))
1174+
colSize := make([]variable.ColSize, len(t.Cols()))
11751175
for id, col := range t.Cols() {
11761176
size, err := codec.EstimateValueSize(sc.TypeCtx(), r[id])
11771177
if err != nil {
11781178
continue
11791179
}
1180-
colSize[col.ID] = int64(size) - 1
1180+
colSize[id] = variable.ColSize{ColID: col.ID, Size: int64(size - 1)}
11811181
}
1182-
sessVars.TxnCtx.UpdateDeltaForTable(t.physicalTableID, 1, 1, colSize)
1182+
sessVars.TxnCtx.UpdateDeltaForTableFromColSlice(t.physicalTableID, 1, 1, colSize)
11831183
return recordID, nil
11841184
}
11851185

@@ -1448,15 +1448,15 @@ func (t *TableCommon) RemoveRecord(ctx table.MutateContext, h kv.Handle, r []typ
14481448
if ctx.GetSessionVars().TxnCtx == nil {
14491449
return nil
14501450
}
1451-
colSize := make(map[int64]int64, len(t.Cols()))
1451+
colSize := make([]variable.ColSize, len(t.Cols()))
14521452
for id, col := range t.Cols() {
14531453
size, err := codec.EstimateValueSize(sc.TypeCtx(), r[id])
14541454
if err != nil {
14551455
continue
14561456
}
1457-
colSize[col.ID] = -int64(size - 1)
1457+
colSize[id] = variable.ColSize{ColID: col.ID, Size: -int64(size - 1)}
14581458
}
1459-
ctx.GetSessionVars().TxnCtx.UpdateDeltaForTable(t.physicalTableID, -1, 1, colSize)
1459+
ctx.GetSessionVars().TxnCtx.UpdateDeltaForTableFromColSlice(t.physicalTableID, -1, 1, colSize)
14601460
return err
14611461
}
14621462

0 commit comments

Comments
 (0)