|
| 1 | +--- |
| 2 | +title: DISTRIBUTE TABLE |
| 3 | +summary: An overview of the usage of DISTRIBUTE TABLE for the TiDB database. |
| 4 | +--- |
| 5 | + |
| 6 | +# DISTRIBUTE TABLE <span class="version-mark">New in v9.0.0</span> |
| 7 | + |
| 8 | +> **Warning:** |
| 9 | +> |
| 10 | +> This feature is experimental. It is not recommended that you use it in the production environment. This feature might be changed or removed without prior notice. If you find a bug, you can report an [issue](https://github.com/pingcap/tidb/issues) on GitHub. |
| 11 | +
|
| 12 | +<CustomContent platform="tidb-cloud"> |
| 13 | + |
| 14 | +> **Note:** |
| 15 | +> |
| 16 | +> This feature is not available on [{{{ .starter }}}](https://docs.pingcap.com/tidbcloud/select-cluster-tier#tidb-cloud-serverless) and [{{{ .essential }}}](https://docs.pingcap.com/tidbcloud/select-cluster-tier#essential) clusters. |
| 17 | +
|
| 18 | +</CustomContent> |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +## Synopsis |
| 23 | + |
| 24 | +```ebnf+diagram |
| 25 | +DistributeTableStmt ::= |
| 26 | + "DISTRIBUTE" "TABLE" TableName PartitionNameListOpt "RULE" EqOrAssignmentEq Identifier "ENGINE" EqOrAssignmentEq Identifier "TIMEOUT" EqOrAssignmentEq Identifier |
| 27 | +
|
| 28 | +TableName ::= |
| 29 | + (SchemaName ".")? Identifier |
| 30 | +
|
| 31 | +PartitionNameList ::= |
| 32 | + "PARTITION" "(" PartitionName ("," PartitionName)* ")" |
| 33 | +``` |
| 34 | + |
| 35 | +## Parameter description |
| 36 | + |
| 37 | +When redistributing Regions in a table 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. |
| 38 | + |
| 39 | +- `RULE`: specifies which Raft role's Region to balance and schedule. Optional values are `"leader-scatter"`, `"peer-scatter"`, and `"learner-scatter"`. |
| 40 | +- `ENGINE`: specifies the storage engine. Optional values are `"tikv"` and `"tiflash"`. |
| 41 | +- `TIMEOUT`: specifies the timeout limit for the scatter operation. If PD does not complete the scatter within this time, the scatter task will automatically exit. When this parameter is not specified, the default value is `"30m"`. |
| 42 | + |
| 43 | +## Examples |
| 44 | + |
| 45 | +Redistribute the Regions of the Leaders in the table `t1` on TiKV: |
| 46 | + |
| 47 | +```sql |
| 48 | +CREATE TABLE t1 (a INT); |
| 49 | +... |
| 50 | +DISTRIBUTE TABLE t1 RULE = "leader-scatter" ENGINE = "tikv" TIMEOUT = "1h"; |
| 51 | +``` |
| 52 | + |
| 53 | +``` |
| 54 | ++--------+ |
| 55 | +| JOB_ID | |
| 56 | ++--------+ |
| 57 | +| 100 | |
| 58 | ++--------+ |
| 59 | +``` |
| 60 | + |
| 61 | +Redistribute the Regions of the Learners in the table `t2` on TiFlash: |
| 62 | + |
| 63 | +```sql |
| 64 | +CREATE TABLE t2 (a INT); |
| 65 | +... |
| 66 | +DISTRIBUTE TABLE t2 RULE = "learner-scatter" ENGINE = "tiflash"; |
| 67 | +``` |
| 68 | + |
| 69 | +``` |
| 70 | ++--------+ |
| 71 | +| JOB_ID | |
| 72 | ++--------+ |
| 73 | +| 101 | |
| 74 | ++--------+ |
| 75 | +``` |
| 76 | + |
| 77 | +Redistribute the Regions of the Peers in the table `t3`'s `p1` and `p2` partitions on TiKV: |
| 78 | + |
| 79 | +```sql |
| 80 | +CREATE TABLE t3 ( a INT, b INT, INDEX idx(b)) PARTITION BY RANGE( a ) ( |
| 81 | + PARTITION p1 VALUES LESS THAN (10000), |
| 82 | + PARTITION p2 VALUES LESS THAN (20000), |
| 83 | + PARTITION p3 VALUES LESS THAN (MAXVALUE) ); |
| 84 | +... |
| 85 | +DISTRIBUTE TABLE t3 PARTITION (p1, p2) RULE = "peer-scatter" ENGINE = "tikv"; |
| 86 | +``` |
| 87 | + |
| 88 | +``` |
| 89 | ++--------+ |
| 90 | +| JOB_ID | |
| 91 | ++--------+ |
| 92 | +| 102 | |
| 93 | ++--------+ |
| 94 | +``` |
| 95 | + |
| 96 | +Redistribute the Regions of the Learner in the table `t4`'s `p1` and `p2` partitions on TiFlash: |
| 97 | + |
| 98 | +```sql |
| 99 | +CREATE TABLE t4 ( a INT, b INT, INDEX idx(b)) PARTITION BY RANGE( a ) ( |
| 100 | + PARTITION p1 VALUES LESS THAN (10000), |
| 101 | + PARTITION p2 VALUES LESS THAN (20000), |
| 102 | + PARTITION p3 VALUES LESS THAN (MAXVALUE) ); |
| 103 | +... |
| 104 | +DISTRIBUTE TABLE t4 PARTITION (p1, p2) RULE = "learner-scatter" ENGINE="tiflash"; |
| 105 | +``` |
| 106 | + |
| 107 | +``` |
| 108 | ++--------+ |
| 109 | +| JOB_ID | |
| 110 | ++--------+ |
| 111 | +| 103 | |
| 112 | ++--------+ |
| 113 | +``` |
| 114 | + |
| 115 | +## Notes |
| 116 | + |
| 117 | +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. |
| 118 | + |
| 119 | +## MySQL compatibility |
| 120 | + |
| 121 | +This statement is a TiDB extension to MySQL syntax. |
| 122 | + |
| 123 | +## See also |
| 124 | + |
| 125 | +- [`SHOW DISTRIBUTION JOBS`](/sql-statements/sql-statement-show-distribution-jobs.md) |
| 126 | +- [`SHOW TABLE DISTRIBUTION`](/sql-statements/sql-statement-show-table-distribution.md) |
| 127 | +- [`SHOW TABLE REGIONS`](/sql-statements/sql-statement-show-table-regions.md) |
| 128 | +- [`CANCEL DISTRIBUTION JOB`](/sql-statements/sql-statement-cancel-distribution-job.md) |
0 commit comments