Skip to content

Commit 0327234

Browse files
Defined2014zeminzhou
authored andcommitted
ddl: let partition table pre split index regions (pingcap#59547)
close pingcap#59530
1 parent 53c727d commit 0327234

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

pkg/ddl/split_region.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ import (
3333

3434
func splitPartitionTableRegion(ctx sessionctx.Context, store kv.SplittableStore, tbInfo *model.TableInfo, parts []model.PartitionDefinition, scatterScope string) {
3535
// Max partition count is 8192, should we sample and just choose some partitions to split?
36-
regionIDs := make([]uint64, 0, len(parts))
36+
var regionIDs []uint64
3737
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), ctx.GetSessionVars().GetSplitRegionTimeout())
3838
defer cancel()
3939
ctxWithTimeout = kv.WithInternalSourceType(ctxWithTimeout, kv.InternalTxnDDL)
4040
if shardingBits(tbInfo) > 0 && tbInfo.PreSplitRegions > 0 {
41+
regionIDs = make([]uint64, 0, len(parts)*(len(tbInfo.Indices)+1))
42+
scatter, tableID := getScatterConfig(scatterScope, tbInfo.ID)
43+
// Try to split global index region here.
44+
regionIDs = append(regionIDs, splitIndexRegion(store, tbInfo, scatter, tableID)...)
4145
for _, def := range parts {
4246
regionIDs = append(regionIDs, preSplitPhysicalTableByShardRowID(ctxWithTimeout, store, tbInfo, def.ID, scatterScope)...)
4347
}
4448
} else {
49+
regionIDs = make([]uint64, 0, len(parts))
4550
for _, def := range parts {
4651
regionIDs = append(regionIDs, SplitRecordRegion(ctxWithTimeout, store, def.ID, tbInfo.ID, scatterScope))
4752
}
@@ -127,7 +132,7 @@ func preSplitPhysicalTableByShardRowID(ctx context.Context, store kv.SplittableS
127132
logutil.DDLLogger().Warn("pre split some table regions failed",
128133
zap.Stringer("table", tbInfo.Name), zap.Int("successful region count", len(regionIDs)), zap.Error(err))
129134
}
130-
regionIDs = append(regionIDs, splitIndexRegion(store, tbInfo, scatter, &tableID)...)
135+
regionIDs = append(regionIDs, splitIndexRegion(store, tbInfo, scatter, physicalID)...)
131136
return regionIDs
132137
}
133138

@@ -146,13 +151,32 @@ func SplitRecordRegion(ctx context.Context, store kv.SplittableStore, physicalTa
146151
return 0
147152
}
148153

149-
func splitIndexRegion(store kv.SplittableStore, tblInfo *model.TableInfo, scatter bool, tableID *int64) []uint64 {
154+
func splitIndexRegion(store kv.SplittableStore, tblInfo *model.TableInfo, scatter bool, physicalTableID int64) []uint64 {
150155
splitKeys := make([][]byte, 0, len(tblInfo.Indices))
151156
for _, idx := range tblInfo.Indices {
152-
indexPrefix := tablecodec.EncodeTableIndexPrefix(tblInfo.ID, idx.ID)
157+
if tblInfo.GetPartitionInfo() != nil &&
158+
((idx.Global && tblInfo.ID != physicalTableID) || (!idx.Global && tblInfo.ID == physicalTableID)) {
159+
continue
160+
}
161+
id := idx.ID
162+
// For normal index, split regions like
163+
// [t_tid_, t_tid_i_idx1ID+1),
164+
// [t_tid_i_idx1ID+1, t_tid_i_idx2ID+1),
165+
// ...
166+
// [t_tid_i_idxMaxID+1, t_tid_r_xxxx)
167+
//
168+
// For global index, split regions like
169+
// [t_tid_i_idx1ID, t_tid_i_idx2ID),
170+
// [t_tid_i_idx2ID, t_tid_i_idx3ID),
171+
// ...
172+
// [t_tid_i_idxMaxID, t_pid1_)
173+
if !idx.Global {
174+
id = id + 1
175+
}
176+
indexPrefix := tablecodec.EncodeTableIndexPrefix(physicalTableID, id)
153177
splitKeys = append(splitKeys, indexPrefix)
154178
}
155-
regionIDs, err := store.SplitRegions(context.Background(), splitKeys, scatter, tableID)
179+
regionIDs, err := store.SplitRegions(context.Background(), splitKeys, scatter, &physicalTableID)
156180
if err != nil {
157181
logutil.DDLLogger().Warn("pre split some table index regions failed",
158182
zap.Stringer("table", tblInfo.Name), zap.Int("successful region count", len(regionIDs)), zap.Error(err))

pkg/ddl/tests/partition/db_partition_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,11 @@ func TestAlterTableTruncatePartitionPreSplitRegion(t *testing.T) {
12321232
PARTITION p3 VALUES LESS THAN (MAXVALUE))`)
12331233
re = tk.MustQuery("show table t2 regions")
12341234
rows = re.Rows()
1235-
require.Len(t, rows, 24)
1235+
require.Len(t, rows, 27)
12361236
tk.MustExec(`alter table t2 truncate partition p3`)
12371237
re = tk.MustQuery("show table t2 regions")
12381238
rows = re.Rows()
1239-
require.Len(t, rows, 24)
1239+
require.Len(t, rows, 27)
12401240
}
12411241

12421242
func TestCreateTableWithKeyPartition(t *testing.T) {

pkg/executor/test/splittest/split_table_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,29 @@ func TestShowTableRegion(t *testing.T) {
575575
}
576576
require.Equal(t, infosync.PlacementScheduleStatePending.String(), rows[i][12])
577577
}
578+
579+
tk.MustExec("drop table if exists sbtest0000")
580+
tk.MustExec("CREATE TABLE sbtest0000(" +
581+
" id bigint," +
582+
" k bigint DEFAULT '0' NOT NULL," +
583+
" c CHAR(120) DEFAULT '' NOT NULL," +
584+
" pad CHAR(60) DEFAULT '' NOT NULL," +
585+
" UNIQUE KEY uk(id) GLOBAL," +
586+
" key idx0(k,c)," +
587+
" key idx1(c))SHARD_ROW_ID_BITS=3 PRE_SPLIT_REGIONS=2 " +
588+
"PARTITION BY RANGE(id)(PARTITION p VALUES LESS THAN (MAXVALUE))")
589+
re = tk.MustQuery("show table sbtest0000 regions")
590+
rows = re.Rows()
591+
require.Len(t, rows, 7) // 3 index regions + 4 record regions
592+
593+
tbl = external.GetTableByName(t, tk, "test", "sbtest0000")
594+
require.Equal(t, fmt.Sprintf("t_%d_i_1_", tbl.Meta().ID), rows[0][1]) // index `uk``
595+
require.Equal(t, fmt.Sprintf("t_%d_i_4_", tbl.Meta().ID+1), rows[1][1])
596+
require.Regexp(t, fmt.Sprintf("t_%d_r_.*", tbl.Meta().ID+1), rows[2][1])
597+
require.Regexp(t, fmt.Sprintf("t_%d_r_.*", tbl.Meta().ID+1), rows[3][1])
598+
require.Regexp(t, fmt.Sprintf("t_%d_r_.*", tbl.Meta().ID+1), rows[4][1])
599+
require.Equal(t, fmt.Sprintf("t_%d_", tbl.Meta().ID+1), rows[5][1]) // index `idx0`
600+
require.Equal(t, fmt.Sprintf("t_%d_i_3_", tbl.Meta().ID+1), rows[6][1]) // index `idx1`
578601
}
579602

580603
func BenchmarkLocateRegion(t *testing.B) {

0 commit comments

Comments
 (0)