注意事項
本文要求PolarDB-X實例版本必須為5.4.14-16539836及以上;
若要對二級分區進行重組操作,要求PolarDB-X實例版本必須為5.4.17-16952556及以上。
哈希類型的分區策略,例如HASH/KEY/CO_HASH,不支持執行該操作。
重組分區時,被重組的分區集合所組成的邊界值范圍定義必須與重組后的新分區的邊界值范圍定義一致。
名詞解釋
表組:分區列完全相同的一組邏輯表或全局索引表的集合。
全局索引:使用另一個維度進行水平分區的數據與主表始終保持強一致的分區表。
語法
ALTER alter_target_definition reorg_partition_operation_definition
alter_target_definition:
TABLE [db_name.]tbl_name
| TABLE tbl_name.index_name
| TABLEGROUP tg_name
| INDEX index_name ON TABLE [db_name.]tbl_name
| TABLEGROUP BY TABLE [db_name.]tbl_name
| TABLEGROUP BY INDEX index_name ON TABLE [db_name.]tbl_name
reorg_partition_operation_definition:
REORGANIZE PARTITION part_name_list INTO (partition_specs_definition)
| REORGANIZE SUBPARTITION subpart_name_list INTO (subpartition_specs_definition)
| MODIFY PARTITION part_name REORGANIZE SUBPARTITION subpart_name_list INTO (subpartition_specs_definition)
part_name_list:
part_name[,part_name,...,]
subpart_name_list:
subpart_name[,subpart_name,...,]
partition_specs_definition:
range_partition_list
| list_partition_list
subpartition_specs_definition:
range_subpartition_list
| list_subpartition_list
range_partition_list:
( range_partition [, range_partition, ... ] )
range_partition:
PARTITION partition_name VALUES LESS THAN (range_bound_value)
# 二級分區的Range/Range Columns分區定義
range_subpartition_list:
( range_subpartition [, range_subpartition, ... ] )
range_subpartition:
SUBPARTITION subpartition_name VALUES LESS THAN (range_bound_value) [partition_spec_options]
list_partition_list:
(list_partition [, list_partition ...])
list_partition:
PARTITION partition_name VALUES IN (list_bound_value) [partition_spec_options]
# 二級分區的List/List Columns分區定義
list_subpartition_list:
(list_subpartition [, list_subpartition ...])
list_subpartition:
SUBPARTITION subpartition_name VALUES IN (list_bound_value) [partition_spec_options]
場景1:目標分區是不含二級分區的一級分區
示例1:重組多個一級RANGE分區
假設r_t1與r_t2都采用Range分區,建表SQL如下所示:
CREATE TABLE `r_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
(
PARTITION p0 VALUES LESS THAN (2020),
PARTITION p1 VALUES LESS THAN (2023),
PARTITION p2 VALUES LESS THAN (2025),
PARTITION p3 VALUES LESS THAN (2027)
);
CREATE TABLE `r_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
(
PARTITION p0 VALUES LESS THAN (2020),
PARTITION p1 VALUES LESS THAN (2023),
PARTITION p2 VALUES LESS THAN (2025),
PARTITION p3 VALUES LESS THAN (2027)
);
給r_t1表的p1、p2、p3這3個連續分區重組為p4、p5,,且p4、p5的定義是( PARTITION p4 VALUES LESS THAN (2024),PARTITION p5 VALUES LESS THAN (2027))
。具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE r_t1 /*表名*/
REORGANIZE PARTITION p1,p2,p3 INTO
(
PARTITION p4 VALUES LESS THAN (2024),
PARTITION p5 VALUES LESS THAN (2027)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作, 假設r_t1與r_t2的表組名字是tg1
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE PARTITION p1,p2,p3 INTO
(
PARTITION p4 VALUES LESS THAN (2024),
PARTITION p5 VALUES LESS THAN (2027)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE r_t1 /*表名*/
REORGANIZE PARTITION p1,p2,p3 INTO
(
PARTITION p4 VALUES LESS THAN (2024),
PARTITION p5 VALUES LESS THAN (2027)
);
示例2:重組多個一級LIST分區
假設l_t1與l_t2都采用List分區,建表SQL如下所示:
CREATE TABLE `l_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY LIST(YEAR(`c`))
(
PARTITION p0 VALUES IN (2020,2022,2024,2026,2028),
PARTITION p1 VALUES IN (2021,2023,2025,2027,2029),
PARTITION p2 VALUES IN (2030,2031,2032)
);
CREATE TABLE `l_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY LIST(YEAR(`c`))
(
PARTITION p0 VALUES IN (2020,2022,2024,2026,2028),
PARTITION p1 VALUES IN (2021,2023,2025,2027,2029),
PARTITION p2 VALUES IN (2030,2031,2032)
);
將l_t1表的p0、p1分區重組為p3、p4,且它們的定義為( PARTITION p3 VALUES IN (2020,2021,2022,2023,2024), PARTITION p4 VALUES IN (2025,2026,2027,2028,2029))
。具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE l_t1 /*表名*/
REORGANIZE PARTITION p0,p1 INTO
(
PARTITION p3 VALUES IN (2020,2021,2022,2023,2024),
PARTITION p4 VALUES IN (2025,2026,2027,2028,2029)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE PARTITION p0,p1 INTO
(
PARTITION p3 VALUES IN (2020,2021,2022,2023,2024),
PARTITION p4 VALUES IN (2025,2026,2027,2028,2029)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE l_t1 /*表名*/
REORGANIZE PARTITION p0,p1 INTO
(
PARTITION p3 VALUES IN (2020,2021,2022,2023,2024),
PARTITION p4 VALUES IN (2025,2026,2027,2028,2029)
);
場景2:目標分區是含有二級分區的一級分區
示例1:重組多個一級分區, 同時指定二級分區定義
該種用法僅適用于非模板化的二級分區。
假設r_k_ntp_t1與r_k_ntp_t2都采用Range+Key的非模板化二級分區,建表SQL如下所示:
CREATE TABLE `r_k_ntp_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
SUBPARTITION BY KEY(a)
(
PARTITION p0 VALUES LESS THAN (2020) SUBPARTITIONS 2,
PARTITION p1 VALUES LESS THAN (2021) SUBPARTITIONS 4,
PARTITION p2 VALUES LESS THAN (2022) SUBPARTITIONS 2,
PARTITION p3 VALUES LESS THAN (2023) SUBPARTITIONS 3
);
CREATE TABLE `r_k_ntp_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
SUBPARTITION BY KEY(a)
(
PARTITION p0 VALUES LESS THAN (2020) SUBPARTITIONS 2,
PARTITION p1 VALUES LESS THAN (2021) SUBPARTITIONS 4,
PARTITION p2 VALUES LESS THAN (2022) SUBPARTITIONS 2,
PARTITION p3 VALUES LESS THAN (2023) SUBPARTITIONS 3
);
將表r_k_ntp_t1的p、p2重組為新的分區p4,p4的定義為PARTITION p2 VALUES LESS THAN (2022) SUBPARTITIONS 4
。具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE r_k_ntp_t1 /*表名*/
REORGANIZE PARTITION p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (2022) SUBPARTITIONS 1
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE PARTITION p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (2022) SUBPARTITIONS 1
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE r_k_ntp_t1 /*表名*/
REORGANIZE PARTITION p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (2022) SUBPARTITIONS 1
);
示例2:重組多個一級分區, 但不指定二級分區定義
該種用法同時適用于模板化二級分區表與非模板化二級分區表。
該種用法與場景1(目標分區是不含二級分區的一級分區)的用法類似。具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE r_k_ntp_t1 /*表名*/
REORGANIZE PARTITION p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (2022)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE PARTITION p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (2022)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE r_k_ntp_t1 /*表名*/
REORGANIZE PARTITION p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (2022)
);
場景3:目標分區是模板化的二級分區
對于模板化二級分區,所有一級分區下的二級分區會同時進行變更。
示例1:重組多個二級RANGE分區
假設k_r_tp_t1與k_r_tp_t2都采用Key+Range的模板化分區,建表SQL如下所示:
CREATE TABLE `k_r_tp_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY KEY(`a`) PARTITIONS 2
SUBPARTITION BY RANGE(`b`)
(
SUBPARTITION sp0 VALUES LESS THAN (1000),
SUBPARTITION sp1 VALUES LESS THAN (2000),
SUBPARTITION sp2 VALUES LESS THAN (3000),
SUBPARTITION sp3 VALUES LESS THAN (4000)
);
CREATE TABLE `k_r_tp_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY KEY(`a`) PARTITIONS 2
SUBPARTITION BY RANGE(`b`)
(
SUBPARTITION sp0 VALUES LESS THAN (1000),
SUBPARTITION sp1 VALUES LESS THAN (2000),
SUBPARTITION sp2 VALUES LESS THAN (3000),
SUBPARTITION sp3 VALUES LESS THAN (4000)
);
給k_r_tp_t1表的二級分區模板的sp1、sp2重組為新sp4、sp5、sp6,且新的分區定義如下:
(
SUBPARTITION sp4 VALUES LESS THAN (2500),
SUBPARTITION sp5 VALUES LESS THAN (2800),
SUBPARTITION sp6 VALUES LESS THAN (3000)
)
具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE k_r_tp_t1 /*表名*/
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION sp4 VALUES LESS THAN (2500),
SUBPARTITION sp5 VALUES LESS THAN (2800),
SUBPARTITION sp6 VALUES LESS THAN (3000)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION sp4 VALUES LESS THAN (2500),
SUBPARTITION sp5 VALUES LESS THAN (2800),
SUBPARTITION sp6 VALUES LESS THAN (3000)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE k_r_tp_t1 /*表名*/
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION sp4 VALUES LESS THAN (2500),
SUBPARTITION sp5 VALUES LESS THAN (2800),
SUBPARTITION sp6 VALUES LESS THAN (3000)
);
示例2:重組多個二級LIST分區
假設k_l_tp_t1與k_l_tp_t2都采用List分區,建表SQL如下所示:
CREATE TABLE `k_l_tp_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY KEY(`a`) PARTITIONS 2
SUBPARTITION BY LIST(YEAR(`c`))
(
SUBPARTITION sp0 VALUES IN (2020,2022,2024,2026,2028),
SUBPARTITION sp1 VALUES IN (2021,2023,2025,2027,2029),
SUBPARTITION sp2 VALUES IN (2030,2031,2032)
);
CREATE TABLE `k_l_tp_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY KEY(`a`) PARTITIONS 2
SUBPARTITION BY LIST(YEAR(`c`))
(
SUBPARTITION sp0 VALUES IN (2020,2022,2024,2026,2028),
SUBPARTITION sp1 VALUES IN (2021,2023,2025,2027,2029),
SUBPARTITION sp2 VALUES IN (2030,2031,2032)
);
給k_l_tp_t1表的二級分區模板sp0、sp1,重組為sp3、sp4,且sp3、sp4的定義如下:
(
SUBPARTITION sp3 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION sp4 VALUES IN (2025,2026,2027,2028,2029)
)
具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE k_l_tp_t1 /*表名*/
REORGANIZE SUBPARTITION sp0,sp1 INTO
(
SUBPARTITION sp3 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION sp4 VALUES IN (2025,2026,2027,2028,2029)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE SUBPARTITION sp0,sp1 INTO
(
SUBPARTITION sp3 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION sp4 VALUES IN (2025,2026,2027,2028,2029)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE k_l_tp_t1 /*表名*/
REORGANIZE SUBPARTITION sp0,sp1 INTO
(
SUBPARTITION sp3 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION sp4 VALUES IN (2025,2026,2027,2028,2029)
);
場景4:目標分區是非模板化的二級分區
對于非模板化二級分區,允許單獨對某個一級分區下的二級分區會進行分區變更。
示例1:重組指定的一級分區的多個二級RANGE分區
假設l_r_ntp_t1與l_r_ntp_t2都采用List+Range的非模板化分區,建表SQL如下所示:
CREATE TABLE `l_r_ntp_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY LIST(`a`)
SUBPARTITION BY RANGE(`b`)
(
PARTITION p0 VALUES IN (2020,2022) (
SUBPARTITION p0sp0 VALUES LESS THAN (1000),
SUBPARTITION p0sp1 VALUES LESS THAN (2000),
SUBPARTITION p0sp2 VALUES LESS THAN (3000),
SUBPARTITION p0sp3 VALUES LESS THAN (4000)
),
PARTITION p1 VALUES IN (2021,2023) (
SUBPARTITION p1sp0 VALUES LESS THAN (1500),
SUBPARTITION p1sp1 VALUES LESS THAN (2500),
SUBPARTITION p1sp2 VALUES LESS THAN (3500),
SUBPARTITION p1sp3 VALUES LESS THAN (4500)
)
);
CREATE TABLE `l_r_ntp_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY LIST(`a`)
SUBPARTITION BY RANGE(`b`)
(
PARTITION p0 VALUES IN (2020,2022) (
SUBPARTITION p0sp0 VALUES LESS THAN (1000),
SUBPARTITION p0sp1 VALUES LESS THAN (2000),
SUBPARTITION p0sp2 VALUES LESS THAN (3000),
SUBPARTITION p0sp3 VALUES LESS THAN (4000)
),
PARTITION p1 VALUES IN (2021,2023) (
SUBPARTITION p1sp0 VALUES LESS THAN (1500),
SUBPARTITION p1sp1 VALUES LESS THAN (2500),
SUBPARTITION p1sp2 VALUES LESS THAN (3500),
SUBPARTITION p1sp3 VALUES LESS THAN (4500)
)
);
給l_r_ntp_t1表的一級分區p0下的二級分區p0sp1、p0sp2重組為p0sp4、p0sp5、p0sp6,新的p0sp4、p0sp5、p0sp6分區的定義為:
(
SUBPARTITION p0sp4 VALUES LESS THAN (2500),
SUBPARTITION p0sp5 VALUES LESS THAN (2800),
SUBPARTITION p0sp6 VALUES LESS THAN (3000)
)
具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE l_r_ntp_t1 /*表名*/
REORGANIZE SUBPARTITION p0sp1,p0sp2 INTO
(
SUBPARTITION p0sp4 VALUES LESS THAN (2500),
SUBPARTITION p0sp5 VALUES LESS THAN (2800),
SUBPARTITION p0sp6 VALUES LESS THAN (3000)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE SUBPARTITION p0sp1,p0sp2 INTO
(
SUBPARTITION p0sp4 VALUES LESS THAN (2500),
SUBPARTITION p0sp5 VALUES LESS THAN (2800),
SUBPARTITION p0sp6 VALUES LESS THAN (3000)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY l_r_ntp_t1 /*表名*/
REORGANIZE SUBPARTITION p0sp1,p0sp2 INTO
(
SUBPARTITION p0sp4 VALUES LESS THAN (2500),
SUBPARTITION p0sp5 VALUES LESS THAN (2800),
SUBPARTITION p0sp6 VALUES LESS THAN (3000)
);
示例2:重組指定的一級分區的多個二級LIST分區
假設r_l_ntp_t1與r_l_ntp_t2都采用List+Range非模板化二級分區,建表SQL如下所示:
CREATE TABLE `r_l_ntp_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
SUBPARTITION BY LIST(`b`)
(
PARTITION p0 VALUES LESS THAN (2020) (
SUBPARTITION p0sp0 VALUES IN (2020,2022,2024,2026,2028),
SUBPARTITION p0sp1 VALUES IN (2021,2023,2025,2027,2029),
SUBPARTITION p0sp2 VALUES IN (2030,2031)
),
PARTITION p1 VALUES LESS THAN (2022) (
SUBPARTITION p1sp0 VALUES IN (2020,2022,2024,2026,2028),
SUBPARTITION p1sp1 VALUES IN (2021,2023,2025,2027,2029),
SUBPARTITION p1sp2 VALUES IN (2030)
)
);
CREATE TABLE `r_l_ntp_t2` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
SUBPARTITION BY LIST(`b`)
(
PARTITION p0 VALUES LESS THAN (2020) (
SUBPARTITION p0sp0 VALUES IN (2020,2022,2024,2026,2028),
SUBPARTITION p0sp1 VALUES IN (2021,2023,2025,2027,2029),
SUBPARTITION p0sp2 VALUES IN (2030,2031)
),
PARTITION p1 VALUES LESS THAN (2022) (
SUBPARTITION p1sp0 VALUES IN (2020,2022,2024,2026,2028),
SUBPARTITION p1sp1 VALUES IN (2021,2023,2025,2027,2029),
SUBPARTITION p1sp2 VALUES IN (2030)
)
);
給l_r_ntp_t1表的一級分區p0下的二級分區p0sp0、p0sp1重組為p0sp4、p0sp5,新的p0sp4、p0sp5 二級分區的定義為:
(
SUBPARTITION p0sp4 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION p0sp5 VALUES IN (2025,2026,2027,2028,2029)
)
具體用法如下所示:
表級用法
## 基于表名字進行表級的分區變更操作
ALTER TABLE r_l_ntp_t1 /*表名*/
REORGANIZE SUBPARTITION p0sp0,p0sp1 INTO
(
SUBPARTITION p0sp4 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION p0sp5 VALUES IN (2025,2026,2027,2028,2029)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE SUBPARTITION p0sp0,p0sp1 INTO
(
SUBPARTITION p0sp4 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION p0sp5 VALUES IN (2025,2026,2027,2028,2029)
);
## 基于表名進行表組級的分區變更操作(即基于表名自動找查對應的表組并進行操作)
ALTER TABLEGROUP BY TABLE r_l_ntp_t1 /*表名*/
REORGANIZE SUBPARTITION p0sp0,p0sp1 INTO
(
SUBPARTITION p0sp4 VALUES IN (2020,2021,2022,2023,2024),
SUBPARTITION p0sp5 VALUES IN (2025,2026,2027,2028,2029)
);
場景5:目標分區是全局索引表的分區
示例1:重組指定的全局索引表的多個一級RANGE分區
假設r_gr_t1是一個使用全局索引的表,建表SQL如下所示:
CREATE TABLE `r_gr_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL,
GLOBAL INDEX `g_r` (`a`) COVERING (`c`)
PARTITION BY RANGE(`a`)
(PARTITION `p0` VALUES LESS THAN (10000),
PARTITION `p1` VALUES LESS THAN (20000),
PARTITION `p2` VALUES LESS THAN (30000),
PARTITION `p3` VALUES LESS THAN (40000))
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
(PARTITION `p0` VALUES LESS THAN (2020),
PARTITION `p1` VALUES LESS THAN (2021));
現在給 r_gr_t1 表的全局索引 g_r 的一級分區 p0,p1,p2 重組為 p4,p5, 且 p4,p5 的定義為
(
PARTITION sp4 VALUES LESS THAN (15000),
PARTITION sp5 VALUES LESS THAN (30000)
)
具體用法如下所示:
表級用法
## 基于 `表名`.`全局索引名` 進行全局索引表的分區變更操作
ALTER TABLE `r_gr_t1`.`g_r`
REORGANIZE PARTITION p0,p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (15000),
PARTITION p5 VALUES LESS THAN (30000)
);
## 基于 `全局索引` ON TALBE `表名` 進行全局索引表的分區變更操作
ALTER INDEX `g_r`/*全局索引名*/ ON TABLE `r_gr_t1` /*表名*/
REORGANIZE PARTITION p0,p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (15000),
PARTITION p5 VALUES LESS THAN (30000)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即上述兩個表同時執行分區變更:
## 基于表組名字進行表組級的分區變更操作,假如全局索引表的表組名是tg1
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE PARTITION p0,p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (15000),
PARTITION p5 VALUES LESS THAN (30000)
);
## 基于 `全局索引` ON TALBE `表名` 進行全局索引表的表組的分區變更操作
ALTER TABLEGROUP BY INDEX `g_r`/*全局索引名*/ ON TABLE `r_gr_t1` /*表名*/
REORGANIZE PARTITION p0,p1,p2 INTO
(
PARTITION p4 VALUES LESS THAN (15000),
PARTITION p5 VALUES LESS THAN (30000)
);
示例2:重組指定的全局索引表的多個二級LIST分區
假設r_g_kl_t1是一個使用全局索引的分區表,其中全局索引使用Key+List的二級分區,建表SQL如下所示:
CREATE TABLE `r_g_kl_t1` (
`a` bigint(20) UNSIGNED NOT NULL,
`b` bigint(20) UNSIGNED NOT NULL,
`c` datetime NOT NULL,
`d` varchar(16) NOT NULL,
`e` varchar(16) NOT NULL,
GLOBAL INDEX `g_kl` (`a`,`b`) COVERING (`c`)
PARTITION BY KEY(`b`) PARTITIONS 2
SUBPARTITION BY LIST(`a`)
(
SUBPARTITION `sp0` VALUES IN (10000,10002,10004,10006,10008),
SUBPARTITION `sp1` VALUES IN (20000,20001,20002,20003,20004),
SUBPARTITION `sp2` VALUES IN (30000,30001,30002)
)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4
PARTITION BY RANGE(YEAR(`c`))
(PARTITION `p0` VALUES LESS THAN (2020),
PARTITION `p1` VALUES LESS THAN (2021));
給r_g_kl_t1表的全局索引g_kl的二級分區模板sp1、sp2重組為新的sp4、sp5,且sp4、sp5的定義如下:
(
SUBPARTITION `sp4` VALUES IN (20000,20001,20002),
SUBPARTITION `sp5` VALUES IN (20003,20004,30000,30001,30002)
)
具體用法如下所示:
表級用法
## 基于 `表名`.`全局索引名` 進行全局索引表的分區變更操作
ALTER TABLE `r_g_kl_t1`.`g_kl`
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION `sp4` VALUES IN (20000,20001,20002),
SUBPARTITION `sp5` VALUES IN (20003,20004,30000,30001,30002)
);
## 基于 `全局索引` ON TALBE `表名` 進行全局索引表的分區變更操作
ALTER INDEX g_kl/*全局索引名*/ ON TABLE `r_g_kl_t1` /*表名*/
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION `sp4` VALUES IN (20000,20001,20002),
SUBPARTITION `sp5` VALUES IN (20003,20004,30000,30001,30002)
);
表組級用法
同一個表組的所有邏輯表同時進行增加分區的操作,即修改全局索引所對應的表組的分區定義:
## 基于表組名字進行表組級的分區變更操作,假如全局索引表的表組名是tg1
ALTER TABLEGROUP tg1 /*表組名*/
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION `sp4` VALUES IN (20000,20001,20002),
SUBPARTITION `sp5` VALUES IN (20003,20004,30000,30001,30002)
);
## 基于 INDEX `全局索引` ON TALBE `表名` 進行全局索引表的表組的分區變更操作
ALTER TABLEGROUP BY INDEX `g_kl`/*全局索引名*/ ON TABLE `r_g_kl_t1` /*表名*/
REORGANIZE SUBPARTITION sp1,sp2 INTO
(
SUBPARTITION `sp4` VALUES IN (20000,20001,20002),
SUBPARTITION `sp5` VALUES IN (20003,20004,30000,30001,30002)
);