key, op, and values fields, as shown below:{"key": "key1", "op": "op1", "values": ["value1","value2"]}
key | op | values |
region | "in", "notIn", "exists", "notExists" | region list, such as ["guangzhou"] |
zone | "in", "notIn", "exists", "notExists" | AZ list, such as ["guangzhou-1"] |
rack | "in", "notIn", "exists", "notExists" | rack list, such as ["rack-1","rack-2"] |
host | "in", "notIn", "exists", "notExists" | host list, such as ["host-1","host-2","host-3"] |
node | "in", "notIn" | node list, such as ["node-tdsql3-xxx-001"] |
replica-count | "=" | number of replicas, such as ["3"] |
follower-count | "=" | number of RG followers, such as ["2"] |
learner-count | "=" | number of RG learners, such as ["1"] |
witness-count | "=" | number of RG witnesses, such as ["1"] |
leader-preferences | "=" | RG leader preference. For usage, refer to note 2 |
fault-tolerance-level | "=" | disaster recovery level, such as ["zone"] |
tdsql-storage-type | "in", "notIn", "exists", "notExists" | disk type list, such as ["CLOUD_TCS","CLOUD_BSSD"] |
key and op fields in the preceding rule table as follows:Rule Field | Corresponding SQL Keyword |
region | REGION |
zone | ZONE |
rack | RACK |
host | HOST |
node | NODE |
replica-count | REPLICA_COUNT |
follower-count | FOLLOWER_COUNT |
learner-count | LEARNER_COUNT |
witness-count | WITNESS_COUNT |
leader-preferences | LEADER_PREFERENCES |
fault-tolerance-level | FAULT_TOLERANCE_LEVEL |
tdsql-storage-type | STORAGE_TYPE |
in / notIn / exists / notExists | IN / NOT IN / EXISTS / NOT EXISTS |
distribution-policy-enabled is enabled by default starting from v18.0.0 and generally does not require manual configuration. If you need to disable this feature, please contact Tencent Cloud technical support for assistance.CREATE DISTRIBUTION POLICY statement to create a DP. A DP can contain multiple rules, which are connected using AND:CREATE DISTRIBUTION POLICY "$policy_name" SET $key1 $op1 ($value1, $value2, ...) AND $key2 $op2 (...) ...;
policy_name and configure one or more DP rules using the SET clause. For the SQL keywords corresponding to each rule, see the keyword mapping table above.ALTER DISTRIBUTION POLICY statement to modify DP rules, and use the RENAME DISTRIBUTION POLICY statement to modify the DP name:# Modify the DP rule named policy_1.ALTER DISTRIBUTION POLICY "policy_1" SET $key $op (...);# Rename the DP old_dp_name to new_dp_name.RENAME DISTRIBUTION POLICY "old_dp_name" TO "new_dp_name";
DROP DISTRIBUTION POLICY statement to delete a DP:DROP DISTRIBUTION POLICY "$policy_name";
# List all created DPsSELECT * FROM information_schema.META_CLUSTER_DPS;# View a specific DP by nameSELECT * FROM information_schema.META_CLUSTER_DPS WHERE distribution_policy_name = '$policy_name';
CREATE DATABASE db1 USING distribution policy ${policy_name};
CREATE TABLE t1(a INT) USING distribution policy ${policy_name};
CREATE TABLE t1(a INT) PARTITION BY HASH(a) PARTITIONS 4 USING distribution policy ${policy_name};
CREATE DISTRIBUTION POLICY "policy_1" SET REPLICA_COUNT = 5;
CREATE DISTRIBUTION POLICY "policy_2" SET REPLICA_COUNT = 4 AND LEARNER_COUNT = 1;
CREATE DISTRIBUTION POLICY "policy_3" SET NODE IN ("node-001", "node-002", "node-003") ANDLEADER_PREFERENCES = (NODE IN ("node-001"));
CREATE DISTRIBUTION POLICY "policy_4" SET STORAGE_TYPE IN ("SSD");
CREATE DISTRIBUTION POLICY "policy_x" SET PARTITION_METHOD = "RANGE" ANDPARTITION_KEY_TO_TIME_TYPE = "predefined:TO_DAYS" ANDEXPIRE = "1 YEAR" ANDSTART_TIME = "2024-06-11 00:11:22" ANDEND_TIME = "2025-06-11 00:11:22" ANDSTORAGE_TYPE IN ("HDD");
PARTITION_METHOD: indicates the partitioning method supported by partitioned tables associated with the DP. Currently, only RANGE and RANGE COLUMNS are supported.PARTITION_KEY_TO_TIME_TYPE: This key is required only for RANGE partitioning and is used to convert partition boundary values into time types. The values corresponding to this key are conversion functions. The system provides three predefined conversion functions: TO_DAYS, UNIX_TIMESTAMP, and YEAR. Users can also define custom conversion functions. A custom function must provide a calculation formula for year, month, day, hour, minute, and second, following the format: year/month/day/hour/minute/second:(a mathematical formula containing the identifier v), where v is the identifier representing the integer value of the partition boundary. For example, if values are set to ["year:v/100", "month:v%100"], the omitted day/hour/minute/second are treated as zero for their respective time units.EXPIRE: Partition expiration time, in the format of a positive integer + unit, such as 1 YEAR. Available units include: YEAR, MONTH, DAY, HOUR.START_TIME: Optional, defaults to the start of the partition. If START_TIME is specified, the DP rule is applied after this time.END_TIME: Optional, defaults to the end of the partition. If END_TIME is specified, the DP rule will be rejected from being applied after this time.# Create policy_x1 using the predefined function UNIX_TIMESTAMPCREATE DISTRIBUTION POLICY "policy_x1" SET PARTITION_METHOD = "RANGE" ANDPARTITION_KEY_TO_TIME_TYPE = ("predefined:UNIX_TIMESTAMP") ANDEXPIRE = "1 MONTH" ANDSTORAGE_TYPE IN ("HDD");# Create a RANGE partitioned table and bind to policy_x1CREATE TABLE t_order_1(id bigint NOT NULL,gmt_modified timestamp NOT NULL)PARTITION BY RANGE(unix_timestamp(gmt_modified))(PARTITION p1 VALUES LESS THAN(unix_timestamp('2025-11-11')),PARTITION p2 VALUES LESS THAN(unix_timestamp('2025-12-11'))) USING DISTRIBUTION POLICY policy_x1;
# Create policy_x2 using a custom functionCREATE DISTRIBUTION POLICY "policy_x2" SET PARTITION_METHOD = "RANGE" ANDPARTITION_KEY_TO_TIME_TYPE = ("year:v/100", "month:v%100") ANDEXPIRE = "1 MONTH" ANDLEADER_PREFERENCES = (ZONE in ("zone1")) ANDSTART_TIME = "2025-12-10 00:00:00";# Create a RANGE partition table and bind it to policy_x2CREATE TABLE t_order_2(id bigint NOT NULL,gmt_modified datetime NOT NULL)PARTITION BY RANGE(YEAR(gmt_modified) * 100 + MONTH(gmt_modified))(PARTITION p1 VALUES LESS THAN(202511),PARTITION p2 VALUES LESS THAN(202512),PARTITION p3 VALUES LESS THAN(202601)) USING DISTRIBUTION POLICY policy_x2;
Custom function: year:v/100, month:v%100The partition boundary value v = YEAR(gmt_modified) * 100 + MONTH(gmt_modified)Example calculation:- p1: v=202511 → year=202511/100=2025, month=202511%100=11- p2: v=202512 → year=202512/100=2025, month=202512%100=12- p3: v=202601 → year=202601/100=2026, month=202601%100=1
Partitioning Operations | Boundary Value | Converted Time | Scheduling Trigger Time | Whether Constrained |
p1 | 202511 | 2025-11-01 00:00:00 | 2025-12-01 00:00:00 | Unconstrained |
p2 | 202512 | 2025-12-01 00:00:00 | 2026-01-01 00:00:00 | Unconstrained |
p3 | 202601 | 2026-01-01 00:00:00 | 2026-02-01 00:00:00 | Constrained |
# Create policy_x3CREATE DISTRIBUTION POLICY "policy_x3" SET PARTITION_METHOD = "RANGE COLUMNS" ANDEXPIRE = "1 YEAR" ANDNODE NOT IN ("node-tdsql3-x-001") ANDSTART_TIME = "2025-12-10 00:00:00" ANDEND_TIME = "2026-12-10 00:00:00";# Create a RANGE COLUMNS partition table and bind to policy_x3CREATE TABLE t_order_3(id bigint NOT NULL,gmt_modified datetime NOT NULL)PARTITION BY RANGE COLUMNS(gmt_modified)(PARTITION p1 VALUES LESS THAN("2024-12-10 00:00:00"),PARTITION p2 VALUES LESS THAN("2025-12-10 00:00:00"),PARTITION p3 VALUES LESS THAN("2026-12-10 00:00:00"),PARTITION p4 VALUES LESS THAN("2027-12-10 00:00:00")) USING DISTRIBUTION POLICY policy_x3;
Partitioning Operations | Boundary Time | Scheduling Trigger Time | Whether Within the Time Window |
p1 | 2024-12-10 | 2025-12-10 | Earlier than START_TIME |
p2 | 2025-12-10 | 2026-12-10 | Within [START_TIME, END_TIME] |
p3 | 2026-12-10 | 2027-12-10 | Within [START_TIME, END_TIME] |
p4 | 2027-12-10 | 2028-12-10 | Later than END_TIME |
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan