PolarDB allows you to seamlessly convert a non-partitioned table into a range-partitioned table without the need to redistribute data. This topic describes how to convert a non-partitioned table into a range-partitioned table in PolarDB.
Background information
When PolarDB converts a non-partitioned table into a range-partitioned table, the data of the non-partitioned table is transferred into the first partition without verification. Other partitions are left empty. In most cases, the feature is used in PolarDB to transfer historical data into the first partition without the need to rewrite data by using the ALTER PARTITION BY
statement. This allows you to quickly convert a non-partitioned table into a range-partitioned table.
For more information about how to quickly convert a non-partitioned table into a range-partitioned table, join DingTalk group 24490017825 to obtain technical support.
Prerequisites
You cluster runs PolarDB for MySQL 8.0.2 whose revision version is 8.0.2.2.10 or later. For information about how to view the version of your cluster, see the "Query the engine version" section of the Engine versions topic.
Limits
If a non-partitioned table contains fields that are added by using the INSTANT ADD COLUMN
statement, you cannot convert the table into a range-partitioned table.
Usage
Convert a non-partitioned table into a range-partitioned table
Syntax
Add the WITHOUT VALIDATION
keyword to the ALTER TABLE statement.
ALTER TABLE table_name
PARTITION BY RANGE {(expr) | COLUMNS(column_list)}
(partition_definition [, partition_definition] ...)
WITHOUT VALIDATION;
partition_definition
:
PARTITION partition_name
VALUES LESS THAN {(value | value_list) | MAXVALUE}
Parameters
Parameter | Description |
table_name | The name of the table. |
column_list | The list of partition key columns. Expressions are not supported. |
RANGE(expr) | The expression used for range partitioning. |
partition_name | The name of the partition. |
value_list | The values of the partition. |
MAXVALUE | The maximum value in the partition. |
Example
Convert the t1
non-partitioned table into a range-partitioned table.
CREATE TABLE t1 (
`a` int ,
`b` int ,
Primary Key(a, b));
insert into t1 values(1,1),(2,1),(3,1),(4,1),(111,111),(3333,333);
alter table t1 partition by range(a) (
partition p0 values less than (100),
partition p1 values less than (200)
) WITHOUT VALIDATION;
All data in the t1
table is transferred into the p0
partition without verification. If you confirm that all data in the non-partitioned table falls within the value range of the p0
partition, you can execute this statement to convert the non-partitioned table into a range-partitioned table.
Make sure that all data in the non-partitioned table falls within the value range of the first partition of the partitioned table. Otherwise, data that falls beyond the value range may not be retrieved after the table is converted into a partitioned table.
Usage in other partitioned tables
The WITHOUT VALIDATION
keyword for converting a non-partitioned table into a range-partitioned table can be used for INTERVAL
-partitioned tables to automatically create RANGE
partitions at the same intervals.
Example
CREATE TABLE t1(
ID int,
DATE DATE,
PRIMARY KEY (ID,DATE)
);
ALTER TABLE t1
partition by RANGE COLUMNS(date) INTERVAL(DAY, 1) (
PARTITION p0 VALUES LESS THAN ('2023-01-31')
) without validation;
Performance
Compared with the mechanism used in native MySQL to convert a non-partitioned table into a partitioned table, the feature used in PolarDB to convert a non-partitioned table into a range-partitioned table only changes the metadata of the table and does not rewrite the table data. Therefore, only less than 0.1 seconds is required to convert the table. The mechanism used in native MySQL to convert a non-partitioned table into a partitioned table requires verification and data rewriting. Therefore, a longer period of time is required. The conversion duration increases based on the size of the table data.
Table size | In native MySQL | In PolarDB |
1 GB (6,001,215 rows) | 52.24 seconds | 0.10 seconds |
10 GB (59,986,052 rows) | 8 minutes 45.82 seconds | 0.07 seconds |