# Split Region 使用文档

## Split Region 的使用

Split Region 有 2 种不同的语法，具体如下：

``````SPLIT TABLE table_name [INDEX index_name] BETWEEN (lower_value) AND (upper_value) REGIONS region_num
``````

`BETWEEN lower_value AND upper_value REGIONS region_num` 语法是通过指定上、下边界和 Region 数量，然后在上、下边界之间均匀切分出 `region_num` 个 Region。

``````SPLIT TABLE table_name [INDEX index_name] BY (value_list) [, (value_list)] ...
``````

`BY value_list…` 语法将手动指定一系列的点，然后根据这些指定的点切分 Region，适用于数据不均匀分布的场景。

### Split Table Region

``````t[table_id]_r[row_id]
``````

``````t22_r11
``````

#### 均匀切分

``````SPLIT TABLE t BETWEEN (-9223372036854775808) AND (9223372036854775807) REGIONS 16;
``````

``````SPLIT TABLE t BETWEEN (0) AND (1000000000) REGIONS 16;
``````

#### 不均匀切分

``````SPLIT TABLE t BY (10000), (90000);
``````

### Split Index Region

``````t[table_id]_i[index_id][index_value]
``````

``````t22_i5abc
``````

#### 均匀切分

`upper``lower` 的值会先编码成 byte 数组，去掉 `lower``upper` byte 数组的最长公共前缀后，从 `lower``upper` 各取前 8 字节转成 uint64，再计算 `step = (upper - lower)/num`。计算出 step 后再将 step 编码成 byte 数组，添加到之前 `upper``lower`的最长公共前缀后面组成一个 key 后去做切分。示例如下：

``````SPLIT TABLE t INDEX idx BETWEEN (-9223372036854775808) AND (9223372036854775807) REGIONS 16;
``````

``````SPLIT TABLE t INDEX idx1 BETWEEN ("a") AND ("z") REGIONS 26;
``````

``````SPLIT TABLE t INDEX idx2 BETWEEN ("2010-01-01 00:00:00") AND ("2020-01-01 00:00:00") REGIONS 10;
``````

``````SPLIT TABLE t INDEX idx3 BETWEEN ("2010-01-01 00:00:00") AND ("2020-01-01 00:00:00") REGIONS 10;
``````

``````SPLIT TABLE t INDEX idx3 BETWEEN ("2010-01-01 00:00:00", "a") AND ("2010-01-01 00:00:00", "z") REGIONS 10;
``````

#### 不均匀切分

``````SPLIT TABLE t1 INDEX idx4 BY ("a", "2000-01-01 00:00:01"), ("b", "2019-04-17 14:26:19"), ("c", "");
``````

``````region1  [ minIndexValue               , ("a", "2000-01-01 00:00:01"))
region2  [("a", "2000-01-01 00:00:01") , ("b", "2019-04-17 14:26:19"))
region3  [("b", "2019-04-17 14:26:19") , ("c", "")                   )
region4  [("c", "")                    , maxIndexValue               )
``````

## pre_split_regions

`pre_split_regions` 必须小于等于 `shard_row_id_bits`

### 示例

``````create table t (a int, b int,index idx1(a)) shard_row_id_bits = 4 pre_split_regions=2;
``````

4 个 table Region 的范围区间如下：

``````region1:   [ -inf      ,  1<<61 )
region2:   [ 1<<61     ,  2<<61 )
region3:   [ 2<<61     ,  3<<61 )
region4:   [ 3<<61     ,  +inf  )
``````

## 相关 session 变量

`SPLIT REGION` 语句相关的 session 变量有 `tidb_wait_split_region_finish``tidb_wait_split_region_timeout`，具体可参考 TiDB 专用系统变量和语法