Skip to content

Latest commit

 

History

History
78 lines (59 loc) · 2.72 KB

sql-statement-create-placement-policy.md

File metadata and controls

78 lines (59 loc) · 2.72 KB
title summary
CREATE PLACEMENT POLICY
TiDB 数据库中 CREATE PLACEMENT POLICY 的使用概况。

CREATE PLACEMENT POLICY

警告:

Placement Rules in SQL 是 TiDB 在 v5.3.0 中引入的实验特性,其语法在 GA 前可能会发生变化,还可能存在 bug。如果你知晓潜在的风险,可通过执行 SET GLOBAL tidb_enable_alter_placement = 1; 来开启该实验特性。

CREATE PLACEMENT POLICY 用于创建命名的放置策略,随后可以将该策略分配给表、分区或数据库。

语法图

CreatePolicyStmt ::=
    "CREATE" "PLACEMENT" "POLICY" IfNotExists PolicyName PlacementOptionList

PolicyName ::=
    Identifier

PlacementOptionList ::=
    DirectPlacementOption
|   PlacementOptionList DirectPlacementOption
|   PlacementOptionList ',' DirectPlacementOption

DirectPlacementOption ::=
    "PRIMARY_REGION" EqOpt stringLit
|   "REGIONS" EqOpt stringLit
|   "FOLLOWERS" EqOpt LengthNum
|   "VOTERS" EqOpt LengthNum
|   "LEARNERS" EqOpt LengthNum
|   "SCHEDULE" EqOpt stringLit
|   "CONSTRAINTS" EqOpt stringLit
|   "LEADER_CONSTRAINTS" EqOpt stringLit
|   "FOLLOWER_CONSTRAINTS" EqOpt stringLit
|   "VOTER_CONSTRAINTS" EqOpt stringLit
|   "LEARNER_CONSTRAINTS" EqOpt stringLit

示例

注意:

如要查看所在集群中可用的区域,见 SHOW PLACEMENT LABELS。如果未看到任何可用的区域,此 TiKV 集群在部署时可能未正确设置标签 (label)。

{{< copyable "sql" >}}

CREATE PLACEMENT POLICY p1 PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4;
CREATE TABLE t1 (a INT) PLACEMENT POLICY=p1;
SHOW CREATE PLACEMENT POLICY p1;
Query OK, 0 rows affected (0.08 sec)

Query OK, 0 rows affected (0.10 sec)

+--------+---------------------------------------------------------------------------------------------------+
| Policy | Create Policy                                                                                     |
+--------+---------------------------------------------------------------------------------------------------+
| p1     | CREATE PLACEMENT POLICY `p1` PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1" FOLLOWERS=4 |
+--------+---------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MySQL 兼容性

该语句是 TiDB 对 MySQL 语法的扩展。

另请参阅