@@ -33,15 +33,20 @@ import (
33
33
34
34
func splitPartitionTableRegion (ctx sessionctx.Context , store kv.SplittableStore , tbInfo * model.TableInfo , parts []model.PartitionDefinition , scatterScope string ) {
35
35
// 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
37
37
ctxWithTimeout , cancel := context .WithTimeout (context .Background (), ctx .GetSessionVars ().GetSplitRegionTimeout ())
38
38
defer cancel ()
39
39
ctxWithTimeout = kv .WithInternalSourceType (ctxWithTimeout , kv .InternalTxnDDL )
40
40
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 )... )
41
45
for _ , def := range parts {
42
46
regionIDs = append (regionIDs , preSplitPhysicalTableByShardRowID (ctxWithTimeout , store , tbInfo , def .ID , scatterScope )... )
43
47
}
44
48
} else {
49
+ regionIDs = make ([]uint64 , 0 , len (parts ))
45
50
for _ , def := range parts {
46
51
regionIDs = append (regionIDs , SplitRecordRegion (ctxWithTimeout , store , def .ID , tbInfo .ID , scatterScope ))
47
52
}
@@ -127,7 +132,7 @@ func preSplitPhysicalTableByShardRowID(ctx context.Context, store kv.SplittableS
127
132
logutil .DDLLogger ().Warn ("pre split some table regions failed" ,
128
133
zap .Stringer ("table" , tbInfo .Name ), zap .Int ("successful region count" , len (regionIDs )), zap .Error (err ))
129
134
}
130
- regionIDs = append (regionIDs , splitIndexRegion (store , tbInfo , scatter , & tableID )... )
135
+ regionIDs = append (regionIDs , splitIndexRegion (store , tbInfo , scatter , physicalID )... )
131
136
return regionIDs
132
137
}
133
138
@@ -146,13 +151,32 @@ func SplitRecordRegion(ctx context.Context, store kv.SplittableStore, physicalTa
146
151
return 0
147
152
}
148
153
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 {
150
155
splitKeys := make ([][]byte , 0 , len (tblInfo .Indices ))
151
156
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 )
153
177
splitKeys = append (splitKeys , indexPrefix )
154
178
}
155
- regionIDs , err := store .SplitRegions (context .Background (), splitKeys , scatter , tableID )
179
+ regionIDs , err := store .SplitRegions (context .Background (), splitKeys , scatter , & physicalTableID )
156
180
if err != nil {
157
181
logutil .DDLLogger ().Warn ("pre split some table index regions failed" ,
158
182
zap .Stringer ("table" , tblInfo .Name ), zap .Int ("successful region count" , len (regionIDs )), zap .Error (err ))
0 commit comments