对表级别的分区实现生命周期管理(TTL),减少用户的使用负担。
在某些使用场景下,用户会将表按照天进行分区划分数据,在没有动态分区功能的时候,用户需要手动管理分区数据。这会给使用方带来额外的维护成本。
通过动态分区功能,用户可以在建表时设定动态分区的规则,FE 会启动一个后台线程,根据用户指定的规则创建或删除分区。
CREATE TABLE tbl1
(...)
PROPERTIES
(
"dynamic_partition.prop1" = "value1",
"dynamic_partition.prop2" = "value2",
...
)
ALTER TABLE tbl1 SET
(
"dynamic_partition.prop1" = "value1",
"dynamic_partition.prop2" = "value2",
...
)
fe.conf
dynamic_partition.参数
动态分区使用过程中,如果因为一些意外情况导致dynamic_partition.start和dynamic_partition.end 之间的某些分区丢失
# 开启动态分区功能
curl --location-trusted -u root:123456 -XGET http://node1:8030/api/_set_config?dynamic_partition_enable=true
# 调度时间设置为5秒,意思是每过5秒根据配置刷新分区,这里设置为5秒,真实场景可以设置为12小时
curl --location-trusted -u root:123456 -XGET http://node1:8030/api/_set_config?dynamic_partition_check_interval_seconds=5
ADMIN SET FRONTEND CONFIG ("dynamic_partition_enable"="true");
ADMIN SET FRONTEND CONFIG ("dynamic_partition_check_interval_seconds"="5");
CREATE TABLE order_dynamic_partition1
(
id int,
time date,
money double,
areaName varchar(50)
)
duplicate key(id,time)
PARTITION BY RANGE(time)()
DISTRIBUTED BY HASH(id) buckets 10
PROPERTIES(
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "DAY",
"dynamic_partition.start" = "-7",
"dynamic_partition.end" = "3",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "10",
"replication_num" = "1"
);
p20230225: ["2023-02-25", "2023-02-26")
p20230226: ["2023-02-26", "2023-02-27")
p20230227: ["2023-02-27", "2023-02-28")
p20230228: ["2023-02-28", "2023-03-01")
show partitions from order_dynamic_partition1;
SHOW DYNAMIC PARTITION TABLES;
insert into order_dynamic_partition1 values(1,'2023-02-25 11:00:00', 200.0, '北京');
insert into order_dynamic_partition1 values(1,'2023-02-26 11:00:00', 200.0, '北京');
insert into order_dynamic_partition1 values(1,'2023-02-27 11:00:00', 200.0, '北京');
show partitions from order_dynamic_partition1;
CREATE TABLE order_dynamic_partition2
(
id int,
time date,
money double,
areaName varchar(50)
)
duplicate key(id,time)
PARTITION BY RANGE(time)()
DISTRIBUTED BY HASH(id) buckets 10
PROPERTIES(
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "WEEK",
"dynamic_partition.start" = "-2",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8"
);
因为当前日期为 2023-02-15,是 2023 年的第8周。默认每周起始为星期一。则根于以上规则,tbl1 会产生以下分区
show partitions from order_dynamic_partition1;
p2023_08: ["2023-02-20", "2023-02-27")
p2023_09: ["2023-02-27", "2023-03-06")
p2023_10: ["2023-03-06", "2023-03-13")
CREATE TABLE order_dynamic_partition3
(
id int,
time date,
money double,
areaName varchar(50)
)
duplicate key(id,time)
PARTITION BY RANGE(time)()
DISTRIBUTED BY HASH(id) buckets 10
PROPERTIES(
"dynamic_partition.enable" = "true",
"dynamic_partition.time_unit" = "MONTH",
"dynamic_partition.end" = "2",
"dynamic_partition.prefix" = "p",
"dynamic_partition.buckets" = "8",
"dynamic_partition.start_day_of_month" = "3"
);
show partitions from order_dynamic_partition3;
p202302: ["2023-02-03", "2023-03-03")
p202303: ["2023-03-03", "2023-04-03")
p202304: ["2023-04-03", "2023-05-03")
MySQL [test_db]> SHOW DYNAMIC PARTITION TABLES\G;
*************************** 1. row ***************************
TableName: order_dynamic_partition2
Enable: true
TimeUnit: WEEK
Start: -2
End: 2
Prefix: p
Buckets: 8
ReplicationNum: 3
ReplicaAllocation: tag.location.default: 3
StartOf: MONDAY
LastUpdateTime: 2023-02-25 17:41:11
LastSchedulerTime: 2023-02-25 18:05:41
State: NORMAL
LastCreatePartitionMsg: NULL
LastDropPartitionMsg: NULL
ReservedHistoryPeriods: NULL
*************************** 2. row ***************************
......
*************************** 3. row ***************************
......