PolarDB for MySQLPolarDBcan quickly convert a non-partitioned table into a range-partitioned table without data redistribution. This topic describes how to usePolarDBto perform the conversion.
Background information
PolarDBWhen you convert a non-partitioned table into a range-partitioned table, the data in the non-partitioned table is placed into the first partition without validation, and other partitions are empty.PolarDBThis method is typically used to archive historical data into the first partition and avoid the data rewrite required by the standardALTER PARTITION BYsyntax, which makes the conversion fast.
Scope of application
MySQL 8.0.2: The revision version must be 8.0.2.2.19 or later.
MySQL 8.0.1: The revision version must be 8.0.1.1.34 or later.
If the table has
INSTANTcolumns or column store indexes (IMCI), the cluster version must beMySQL 8.0.2 and the revision version must be 8.0.2.2.36 or later.
You canQuery the engine versionquery the engine version to check the cluster version.
Limitations
If a table has columns added by theINSTANT ADD COLUMN statement, the table cannot be converted into a range-partitioned table.
Instructions
Syntax and examples
Syntax
In theALTER TABLE statement, add the
WITHOUT VALIDATIONkeyword.ALTER TABLE table_name PARTITION BY RANGE {(expr) | COLUMNS(column_list)} (partition_definition [, partition_definition] ...) WITHOUT VALIDATION;where
partition_definition:PARTITION partition_name VALUES LESS THAN {(value | value_list) | MAXVALUE}Parameters
Parameter
Parameters
table_name
The name of the table.
column_list
LIST COLUMNS type. The list of partition keys. Expressions are not supported.
RANGE(expr)
RANGEThe expression used for range partitioning.
partition_name
The name of the partition.
value_list
The boundary values of the partition.
MAXVALUE
The maximum value.
Example
Convert the non-partitioned table
t1into 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;t1, all data is moved topartition p0without validation. If you confirm that all data in the non-partitioned table falls withinp0declared boundary, you can use this syntax to quickly convert the non-partitioned table into a range-partitioned table.NoteMake sure that all data in the non-partitioned table falls within the boundary of the first partition. Otherwise, after the conversion, data that does not meet the partitioning rules may not be retrieved.
When the WITHOUT VALIDATION option is used, all data in the non-partitioned table is moved to the first partition without validation. Make sure that the partition boundary specified in the DDL statement matches all data in the non-partitioned table.
Combined syntax
WITHOUT VALIDATION can be used withINTERVAL partitioned tables, which automatically createRANGE partitions at the same interval. 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 comparison
PolarDBCompared with the native MySQL method, converting a non-partitioned table into a range-partitioned table in PolarDB for MySQL updates only the table metadata and does not rewrite data. The conversion completes in less than 0.1 seconds. The native MySQL method must validate and rewrite data, which takes longer as the data volume increases.
Table size | Standard MySQL ALTER TABLE PARTITION BY | PolarDBSyntax and examples |
1 GB (6001215 rows) | 52.24 seconds | 0.10 seconds |
10 GB (59986052 rows) | 8 minutes45.82 seconds | 0.07 seconds |