|
| 1 | +--- |
| 2 | +title: DISTRIBUTE TABLE |
| 3 | +summary: An overview of the usage of DISTRIBUTE TABLE for the TiDB database. |
| 4 | +--- |
| 5 | + |
| 6 | +# DISTRIBUTE TABLE |
| 7 | + |
| 8 | +The `DISTRIBUTE TABLE` statement redistributes and reschedules Regions of a specified table to achieve a balanced distribution at the table level. Executing this statement helps prevent Regions from being concentrated on a few TiFlash or TiKV nodes, addressing the issue of uneven region distribution in the table. |
| 9 | + |
| 10 | +## Syntax |
| 11 | + |
| 12 | +```ebnf+diagram |
| 13 | +DistributeTableStmt ::= |
| 14 | + "DISTRIBUTE" "TABLE" TableName PartitionNameList? EngineOption? RoleOption? |
| 15 | +
|
| 16 | +TableName ::= |
| 17 | + (SchemaName ".")? Identifier |
| 18 | +
|
| 19 | +PartitionNameList ::= |
| 20 | + "PARTITION" "(" PartitionName ("," PartitionName)* ")" |
| 21 | +
|
| 22 | +EngineOption ::= |
| 23 | + "ENGINE" Expression |
| 24 | +
|
| 25 | +RoleOption ::= |
| 26 | + "Role" Expression |
| 27 | +``` |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +When redistributing Regions using the `DISTRIBUTE TABLE` statement, you can specify the storage engine (such as TiFlash or TiKV) and different Raft roles (such as Leader, Learner, or Voter) for balanced distribution. |
| 32 | + |
| 33 | +Redistribute the Regions of the Leader in the table `t1` on TiKV: |
| 34 | + |
| 35 | +```sql |
| 36 | +CREATE TABLE t1 (a INT); |
| 37 | +... |
| 38 | +DISTRIBUTE TABLE t1 engine tikv role leader |
| 39 | +``` |
| 40 | + |
| 41 | +``` |
| 42 | ++---------+ |
| 43 | +| JOB_ID | |
| 44 | +100 |
| 45 | ++---------+ |
| 46 | +``` |
| 47 | + |
| 48 | +Redistribute the Regions of the Learner in the table `t2` on TiFlash: |
| 49 | + |
| 50 | +```sql |
| 51 | +CREATE TABLE t2 (a INT); |
| 52 | +... |
| 53 | +DISTRIBUTE TABLE t2 ENGINE tiflash role learner; |
| 54 | +``` |
| 55 | + |
| 56 | +``` |
| 57 | ++---------+ |
| 58 | +| JOB_ID | |
| 59 | +101 |
| 60 | ++---------+ |
| 61 | +``` |
| 62 | + |
| 63 | +Redistribute the Regions of the Leader in the table `t3`'s `p1` and `p2` partitions on TiKV: |
| 64 | + |
| 65 | +```sql |
| 66 | +CREATE TABLE t3 (a INT); |
| 67 | +... |
| 68 | +DISTRIBUTE TABLE t3 PARTITION (p1, p2) ENGINE tikv role leader; |
| 69 | +``` |
| 70 | + |
| 71 | +``` |
| 72 | ++---------+ |
| 73 | +| JOB_ID | |
| 74 | +102 |
| 75 | ++---------+ |
| 76 | +``` |
| 77 | + |
| 78 | +Execute the [`SHOW DISTRIBUTION JOBS`](/sql-statements/sql-statement-show-distribution-jobs.md) statement to view all distribution jobs: |
| 79 | + |
| 80 | +```sql |
| 81 | +SHOW DISTRIBUTION JOBS; |
| 82 | +``` |
| 83 | + |
| 84 | +``` |
| 85 | ++---------+------------+------------+-----------------+------------+-----------+----------+-------------+---------------+ |
| 86 | +| JOB_ID | DB_NAME | TABLE_NAME | PARTITION_NAMES | ENGINE_TYPE | ROLE_TYPE | STATUS | CREATE_USER | CREATE_TIME | |
| 87 | ++---------+------------+------------+-----------------+------------+-----------+--------+---------------+---------------+ |
| 88 | +| 1 | db_1 | t1 | | TIKV | LEADER | RUNNING | ADMIN | 20240712 | |
| 89 | +| 2 | db_1 | t2 | | TIFLASH | LEARNER | FINISHED | ADMIN | 20240715 | |
| 90 | +| 3 | db_1 | t3 | | TiKV | VOTER | STOPPED | ADMIN | 20240713 | |
| 91 | +| 4 | db_1 | t4 | | TIFLASH | LEARNER | FINISHED | ADMIN | 20240713 | |
| 92 | ++---------+------------+------------+-----------------+------------+-----------+----------+-------------+---------------+ |
| 93 | +``` |
| 94 | + |
| 95 | +Execute the [`SHOW TABLE DISTRIBUTION`](/sql-statements/sql-statement-show-table-distribution.md) statement to view the Region distribution of the table `t1`: |
| 96 | + |
| 97 | +```sql |
| 98 | +SHOW TABLE DISTRIBUTION t1; |
| 99 | +``` |
| 100 | + |
| 101 | +``` |
| 102 | ++---------+------------+----------------+----------+------------+-------------------+--------------------+-----------------+------------------+ |
| 103 | +| DB_NAME | TABLE_NAME | PARTITION_NAME | STORE_ID | STORE_TYPE | REGION_LEADER_NUM | REGION_LEADER_BYTE | REGION_PEER_NUM | REGION_PEER_BYTE | |
| 104 | ++---------+------------+----------------+----------+------------+-------------------+--------------------+-----------------+------------------+ |
| 105 | +| db_1 | t1 | | 1 | TiKV | 315 | 24057934521 | 1087 | 86938746542 | |
| 106 | +| db_1 | t1 | | 2 | TiKV | 324 | 28204839240 | 1104 | 91039476832 | |
| 107 | +| db_1 | t1 | | 3 | TiKV | 319 | 25986274812 | 1091 | 89405367423 | |
| 108 | +| db_1 | t1 | | 4 | TiKV | 503 | 41039587625 | 1101 | 90482317797 | |
| 109 | ++---------+------------+----------------+----------+------------+-------------------+--------------------+-----------------+------------------+ |
| 110 | +``` |
| 111 | + |
| 112 | +## Notes |
| 113 | + |
| 114 | +When you execute the `DISTRIBUTE TABLE` statement to redistribute Regions of a table, the Region distribution result might be affected by the PD hotspot scheduler. After the redistribution, the Region distribution of this table might become imbalanced again over time. |
| 115 | + |
| 116 | +## MySQL compatibility |
| 117 | + |
| 118 | +This statement is a TiDB extension to MySQL syntax. |
| 119 | + |
| 120 | +## See also |
| 121 | + |
| 122 | +- [`SHOW DISTRIBUTION JOBS`](/sql-statements/sql-statement-show-distribution-jobs.md) |
| 123 | +- [`SHOW TABLE DISTRIBUTION`](/sql-statements/sql-statement-show-table-distribution.md) |
| 124 | +- [`SHOW TABLE REGIONS`](/sql-statements/sql-statement-show-table-regions.md) |
0 commit comments