二级分区表创建成功后,您可以对二级分区表进行添加、删除或 Truncate 操作。
添加一级分区
语法
二级分区表添加一级分区主要有为模板化二级分区表和非模板化二级分区添加一级分区表两种场景。
模板化二级分区表
ALTER TABLE table_name ADD PARTITION partition_options; partition_options: {PARTITION partition_name VALUES LESS THAN range_partition_expr [,PARTITION partition_name VALUES LESS THAN range_partition_expr]... } |{PARTITION partition_name VALUES IN list_partition_expr [,PARTITION partition_name VALUES IN list_partition_expr]...}说明
对于模板化二级分区表,添加一级分区时只需要指定一级分区的定义即可,二级分区的定义会⾃动按照模板填充。
非模板化二级分区表
ALTER TABLE table_name ADD PARTITION partition_options; partition_options: {PARTITION partition_name VALUES LESS THAN range_partition_expr (subpartition_option) [,PARTITION partition_name VALUES LESS THAN range_partition_expr (subpartition_option)]... } |{PARTITION partition_name VALUES IN list_partition_expr (subpartition_option) [,PARTITION partition_name VALUES IN list_partition_expr (subpartition_option)]...} subpartition_option: {SUBPARTITION subpartition_name VALUES LESS THAN range_partition_expr, ...} |{SUBPARTITION subpartition_name VALUES IN list_partition_expr, ....} |{SUBPARTITION subpartition_name, ....}说明
对于非模板化二级分区表,添加一级分区时,需要同时指定一级分区的定义和该一级分区下的二级分区定义。
示例
向 Range Columns + Range Columns 模板化分区表
t_m_rcrc中添加一级分区p3和p4。obclient> ALTER TABLE t_m_rcrc ADD PARTITION (PARTITION p3 VALUES LESS THAN(400), PARTITION p4 VALUES LESS THAN(500) ); Query OK, 0 rows affected查看分区表
t_m_rcrc中添加一级分区p3和p4。obclient> show create table t_m_rcrc\G *************************** 1. row *************************** Table: t_m_rcrc Create Table: CREATE TABLE `t_m_rcrc` ( `col1` int(11) DEFAULT NULL, `col2` int(11) DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range columns(col1) subpartition by range columns(col2) subpartition template ( subpartition mp0 values less than (1000), subpartition mp1 values less than (2000), subpartition mp2 values less than (3000)) (partition p0 values less than (100), partition p1 values less than (200), partition p2 values less than (300), partition p3 values less than (400), partition p4 values less than (500)) 1 row in set插入数据到
t_m_rcrc表的p3和p4分区。obclient> INSERT INTO t_m_rcrc VALUES (399,399),(499,499); Query OK, 2 row affected查看已新建的分区表
t_m_rcrc的p3和p4分区。obclient> SELECT * FROM t_m_rcrc partition(p3,p4); +------+------+ | col1 | col2 | +------+------+ | 399 | 399 | | 499 | 499 | +------+------+ 2 rows in set向 Range + Range 非模板化分区表
t2_f_rclc中添加一级分区p2。obclient> ALTER TABLE t2_f_rclc ADD PARTITION (PARTITION p2 VALUES LESS THAN(300) (SUBPARTITION sp6 VALUES IN(1,3), SUBPARTITION sp7 VALUES IN(4,6), SUBPARTITION sp8 VALUES IN(7,9)) ); Query OK, 0 rows affected查看分区表
t2_f_rclc中添加一级分区p6和p7、p8。obclient> show create table t2_f_rclc\G *************************** 1. row *************************** Table: t2_f_rclc Create Table: CREATE TABLE `t2_f_rclc` ( `col1` int(11) DEFAULT NULL, `col2` int(11) DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range columns(col1) subpartition by list columns(col2) (partition p0 values less than (100) ( subpartition sp0 values in (1,3), subpartition sp1 values in (4,6), subpartition sp2 values in (7,9)), partition p1 values less than (200) ( subpartition sp3 values in (1,3), subpartition sp4 values in (4,6), subpartition sp5 values in (7,9)), partition p2 values less than (300) ( subpartition sp6 values in (1,3), subpartition sp7 values in (4,6), subpartition sp8 values in (7,9))) 1 row in set插入数据到
t2_f_rclc表的p6和p7、p8分区。obclient> INSERT INTO t2_f_rclc VALUES (299,3),(299,6),(299,9); Query OK, 1 row affected查看已新建的分区表
t2_f_rclc表的p6和p7、p8分区。obclient> SELECT * FROM t2_f_rclc partition(sp6,sp7,sp8); +------+------+ | col1 | col2 | +------+------+ | 299 | 3 | | 299 | 6 | | 299 | 9 | +------+------+ 3 rows in set
添加二级分区
当前 MySQL 模式暂不支持向表中添加二级分区。
删除一级分区
语法
ALTER TABLE table_name DROP PARTITION partition_name[, partition_name ...];
说明
- 删除二级分区表中的一级分区时,请尽量避免该分区上存在活动的事务或查询,否则可能会导致 SQL 语句报错,或者出现一些异常情况。在
sys租户下,通过事务状态表__all_virtual_trans_stat可以查询到当前还未结束的事务上下文状态。- 删除一级分区会同时删除该一级分区的定义和其对应的二级分区及数据。
示例
删除 Range Columns + Range Columns 模板化分区表
t_m_rcrc中的一级分区p3和p4。查看
t_m_rcrc分区表。obclient> show create table t_m_rcrc\G *************************** 1. row *************************** Table: t_m_rcrc Create Table: CREATE TABLE `t_m_rcrc` ( `col1` int(11) DEFAULT NULL, `col2` int(11) DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range columns(col1) subpartition by range columns(col2) subpartition template ( subpartition mp0 values less than (1000), subpartition mp1 values less than (2000), subpartition mp2 values less than (3000)) (partition p0 values less than (100), partition p1 values less than (200), partition p2 values less than (300), partition p3 values less than (400), partition p4 values less than (500)) 1 row in set (0.00 sec)删除
t_m_rcrc中的一级分区p3和p4。obclient> ALTER TABLE t_m_rcrc DROP PARTITION p3,p4; Query OK, 0 rows affected查看运行结果。
obclient> show create table t_m_rcrc\G *************************** 1. row *************************** Table: t_m_rcrc Create Table: CREATE TABLE `t_m_rcrc` ( `col1` int(11) DEFAULT NULL, `col2` int(11) DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range columns(col1) subpartition by range columns(col2) subpartition template ( subpartition mp0 values less than (1000), subpartition mp1 values less than (2000), subpartition mp2 values less than (3000)) (partition p0 values less than (100), partition p1 values less than (200), partition p2 values less than (300) 1 row in set (0.00 sec)
删除 Range + Range 非模板化分区表
t2_f_rr中的一级分区p2。查看
t_m_rcrc分区表。obclient> show create table t2_f_rr\G *************************** 1. row *************************** Table: t2_f_rr Create Table: CREATE TABLE `t2_f_rr` ( `col1` int(11) DEFAULT NULL, `col2` timestamp NULL DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2)) (partition p0 values less than (100) ( subpartition sp0 values less than (1617206400), subpartition sp1 values less than (1625068800), subpartition sp2 values less than (1633017600), subpartition sp3 values less than (1640966400)), partition p1 values less than (200) ( subpartition sp4 values less than (1617206400), subpartition sp5 values less than (1625068800), subpartition sp6 values less than (1633017600), subpartition sp7 values less than (1643644800)), partition p2 values less than (300) ( subpartition sp8 values less than (1617206400), subpartition sp9 values less than (1625068800), subpartition sp10 values less than (1633017600), subpartition sp11 values less than (1643644800))) 1 row in set (0.00 sec)删除
t2_f_rr中的一级分区p2。obclient> ALTER TABLE t2_f_rr DROP PARTITION P2; Query OK, 0 rows affected查看运行结果。
obclient> show create table t2_f_rr\G *************************** 1. row *************************** Table: t2_f_rr Create Table: CREATE TABLE `t2_f_rr` ( `col1` int(11) DEFAULT NULL, `col2` timestamp NULL DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2)) (partition p0 values less than (100) ( subpartition sp0 values less than (1617206400), subpartition sp1 values less than (1625068800), subpartition sp2 values less than (1633017600), subpartition sp3 values less than (1640966400)), partition p1 values less than (200) ( subpartition sp4 values less than (1617206400), subpartition sp5 values less than (1625068800), subpartition sp6 values less than (1633017600), subpartition sp7 values less than (1643644800))) 1 row in set (0.00 sec)
删除二级分区
语法
ALTER TABLE table_name DROP SUBPARTITION subpartition_name[, subpartition_name ...];
说明*
- 删除二级分区表中的二级分区时,请尽量避免该分区上存在活动的事务或查询,否则可能会导致 SQL 语句报错,或者出现一些异常情况。在
sys租户下,通过事务状态表__all_virtual_trans_stat可以查询到当前还未结束的事务上下文状态。- 删除二级分区会同时删除该分区的定义和其中的数据。
- 当删除多个二级分区时,这些二级分区必须属于同一个一级分区。
示例
删除 Range + Range 非模板化分区表 t2_f_rr 中的二级分区 sp6 和 sp7。
查看
t2_f_rr分区表。obclient> show create table t2_f_rr\G *************************** 1. row *************************** Table: t2_f_rr Create Table: CREATE TABLE `t2_f_rr` ( `col1` int(11) DEFAULT NULL, `col2` timestamp NULL DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2)) (partition p0 values less than (100) ( subpartition sp0 values less than (1617206400), subpartition sp1 values less than (1625068800), subpartition sp2 values less than (1633017600), subpartition sp3 values less than (1640966400)), partition p1 values less than (200) ( subpartition sp4 values less than (1617206400), subpartition sp5 values less than (1625068800), subpartition sp6 values less than (1633017600), subpartition sp7 values less than (1643644800))) 1 row in set (0.00 sec)t2_f_rr中的二级分区sp6和sp7。obclient> ALTER TABLE t2_f_rr DROP SUBPARTITION sp6,sp7; Query OK, 0 rows affected查看运行结果。
obclient> show create table t2_f_rr\G *************************** 1. row *************************** Table: t2_f_rr Create Table: CREATE TABLE `t2_f_rr` ( `col1` int(11) DEFAULT NULL, `col2` timestamp NULL DEFAULT NULL ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2)) (partition p0 values less than (100) ( subpartition sp0 values less than (39283200), subpartition sp1 values less than (47145600), subpartition sp2 values less than (55094400), subpartition sp3 values less than (63043200)), partition p1 values less than (200) ( subpartition sp4 values less than (39283200), subpartition sp5 values less than (47145600))) 1 row in set
Truncate 一级分区
语法
ALTER TABLE table_name TRUNCATE PARTITION partition_name[, partition_name ...];
说明
- Truncate 二级分区表中的一级分区时,请尽量避免该分区上存在活动的事务或查询,否则可能会导致 SQL 语句报错,或者出现一些异常情况。在
sys租户下,通过事务状态表__all_virtual_trans_stat可以查询到当前还未结束的事务上下文状态。- OceanBases 数据库当前支持对 Range/List [Columns] 类型(组合)的二级分区表中的一级分区执行 Truncate 操作,将一个或多个一级分区中对应的二级分区的数据全部移除。
示例
清空 Range Columns + List Columns 分区表 t2_f_rclc 的一级分区 p0 中的数据。
t2_f_rclc的一级分区p0中的数据。obclient> select * from t2_f_rclc partition(p0); +------+------+ | col1 | col2 | +------+------+ | 1 | 6 | +------+------+ 1 row in set (0.01 sec)删除
t2_f_rclc的一级分区p0中的数据。obclient> ALTER TABLE t2_f_rclc TRUNCATE PARTITION p0; Query OK, 0 rows affected查看运行结果。
obclient> select * from t2_f_rclc partition(p0); Empty set (0.01 sec)
Truncate 二级分区
语法
ALTER TABLE table_name TRUNCATE SUBPARTITION subpartition_name[, subpartition_name ...];
说明
- Truncate 二级分区表中的二级分区时,请尽量避免该分区上存在活动的事务或查询,否则可能会导致 SQL 语句报错,或者出现一些异常情况。在
sys租户下,通过事务状态表__all_virtual_trans_stat可以查询到当前还未结束的事务上下文状态。- OceanBases 数据库支持对 Range/List [Columns] 类型(组合)的二级分区表中的二级分区执行 Truncate 操作,将一个或多个二级分区中的数据全部移除。
- 当清除多个二级分区时,这些二级分区必须属于同一个一级分区。
示例
清空 Range + Range 分区表 t2_f_rr 的二级分区 sp1 和 sp2 中的数据。
查看
t2_f_rr的二级分区 sp1 和 sp2 中的数据。obclient> select * from t2_f_rr partition(sp1,sp2); +------+---------------------+ | col1 | col2 | +------+---------------------+ | 10 | 2021-07-01 00:00:00 | | 11 | 2021-09-30 00:00:00 | +------+---------------------+ 2 rows in set (0.00 sec)清空
t2_f_rr的二级分区 sp1 和 sp2 中的数据。obclient> ALTER TABLE t2_f_rr TRUNCATE SUBPARTITION sp1,sp2; Query OK, 0 rows affected查看运行结果。
obclient> select * from t2_f_rr partition(sp1,sp2); Empty set (0.01 sec)