Modifying Partitioned Tables
This section describes the syntax used to modify partitioned tables.
| Syntax | Description |
|---|---|
| Adds partitions and subpartitions to an existing partitioned table. | |
| Deletes partitions and subpartitions along with the stored data. | |
| Removes all data from a specified partition or subpartition while preserving its subpartition structure. | |
| Reduces the number of HASH and KEY partitions and their subpartitions and merges the data into other partitions and subpartitions. | |
| Reorganizes partitions in LIST or RANGE partitioned tables (such as merging, splitting, or modifying partitions) and automatically redistributes data without loss. | |
| Exchanges a partition or subpartition of a partitioned table with a non-partitioned table that has the same schema. | |
| Updates statistics for a partition or subpartition. | |
| Checks a partition or subpartition and indicates whether its data or indexes are corrupted. | |
| Optimizes a partition or subpartition, reclaims unused space, and defragments partition data files. | |
| Rebuilds a partition. | |
| Repairs a corrupted partition or subpartition. | |
| Removes partition and subpartition structures from a partitioned table. |
ADD PARTITION
Description
Adds partitions and subpartitions to an existing partitioned table.
Syntax
ALTER TABLE…ADD PARTITION adds partitions and subpartitions to an existing partitioned table and the table must already be subpartitioned.
New partitions and subpartitions must use the same partitioning type as the existing ones. The new partitioning rules must reference and define the same columns specified in the existing partitioning rules.
ALTER TABLE table_name ADD PARTITION partition_definition;
{list_partition | range_partition | hash_partition | key_partition} PARTITION [partition_name] VALUES IN (value[, value]...) [TABLESPACE tablespace_name], (subpartition, ...)
PARTITION partition_name VALUES LESS THAN (value[, value]...) [TABLESPACE tablespace_name] [(subpartition, ...)]
PARTITION partition_name [TABLESPACE tablespace_name] (subpartition, ...)
{list_subpartition | range_subpartition | hash_partition | key_partition} SUBPARTITION [subpartition_name] VALUES IN (value[, value]...) [TABLESPACE tablespace_name]
SUBPARTITION [subpartition_name ] VALUES LESS THAN (value[, value]...) [TABLESPACE tablespace_name]
SUBPARTITION [subpartition_name ] [TABLESPACE tablespace_name]
| Parameter | Description |
|---|---|
| table_name | Name of the table to which partitions are added. |
| partition_name | Name of the partition to be created. Partition names must be unique across all partitions and subpartitions and follow object identifier naming conventions. |
| subpartition_name | Name of the subpartition to be created. Subpartition names must be unique across all partitions and subpartitions and follow object identifier naming conventions. |
| (value[, value]...) | A quoted literal value (or a comma-separated list of literal values) used to determine how table entries are divided into different partitions. Each partition rule must specify at least one value, with no upper limit on the number of specified values. value can be null, default (for a LIST partition), or maxvalue (for a RANGE partition). |
| tablespace_name | Name of the tablespace to which the partition or subpartition belongs. |
Examples
- Assume that the database has a partitioned table named sales_order. The statement for creating the table is as follows:
CREATE TABLE sales_order ( region_id INT, product_id INT, city varchar(20), order_date DATE, quantity INT ) PARTITION BY RANGE(region_id) SUBPARTITION BY RANGE(product_id) ( PARTITION r0 VALUES LESS THAN (1000) ( SUBPARTITION s0 VALUES LESS THAN(100), SUBPARTITION s1 VALUES LESS THAN(200), SUBPARTITION s2 VALUES LESS THAN(300), SUBPARTITION s3 VALUES LESS THAN(MAXVALUE) ) ); - Run ALTER TABLE...ADD PARTITION to add a partition and subpartitions to the sales_order table:
ALTER TABLE sales_order ADD PARTITION ( PARTITION r1 VALUES less than (3000) ( SUBPARTITION s0 VALUES LESS THAN(100), SUBPARTITION s1 VALUES LESS THAN(200), SUBPARTITION s2 VALUES LESS THAN(300), SUBPARTITION s3 VALUES LESS THAN(MAXVALUE) ) );
DROP PARTITION
Description: deletes partitions and subpartitions along with the stored data.
Syntax
ALTER TABLE table_name DROP PARTITION partition_names;
This statement cannot drop subpartitions individually or HASH and KEY partitions.
| Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_names | Names of the partitions to delete. |
Examples
Drop partition s1 from the sales_order table:
ALTER TABLE sales_order DROP PARTITION s1;
TRUNCATE PARTITION
Description: removes all data from a specified partition or subpartition while preserving its subpartition structure.
Syntax
ALTER TABLE table_name TRUNCATE PARTITION partition_name [,partition_name] ...
When this statement is executed on a subpartitioned table with a partition name specified, all subpartitions belonging to that partition are included in this operation.
partition_name
{partition_name | subpartition_name} | Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_name | Name of the partition to truncate. |
| subpartition_name | Name of the subpartition to truncate. |
Examples
- Truncate data in partition r1 of the part_range_list table:
ALTER TABLE part_range_list TRUNCATE PARTITION r1;
- Truncate data in subpartition s0 belonging to partition r1 of the part_range_list table:
ALTER TABLE part_range_list TRUNCATE PARTITION r1_s0;
COALESCE PARTITION
Description: reduces the number of HASH and KEY partitions and their subpartitions and merges the data into other partitions and subpartitions.
Syntax
ALTER TABLE table_name COALESCE PARTITION num;
| Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| num | Number of partitions to reduce. It must be less than the total number of table partitions. |
Examples
- Reduce two partitions from the part_hash_hash table:
ALTER TABLE part_hash_hash COALESCE PARTITION 2;
- Reduce two partitions from the part_key_key table:
ALTER TABLE part_key_key COALESCE PARTITION 2;
REORGANIZE PARTITION
Description: reorganizes partitions in LIST or RANGE partitioned tables (such as merging, splitting, or modifying partitions) and automatically redistributes data without loss.
Syntax
ALTER TABLE table_name
REORGANIZE PARTITION partition_names INTO (partition_definitions)
partition_definitions: {list_partition | range_partition}
subpartition_definition: {list_subpartition | range_subpartition | hash_subpartition | key_subpartition} | Parameter | Description |
|---|---|
| table_name | Name of the table. |
| partition_names | A comma-separated list of existing partition names to merge or split. |
| partition_definitions | A comma-separated list of new partition definitions. |
| partition_name | Name of the partition to create. Note Partition names must be unique across all partitions and subpartitions and follow object identifier naming conventions. |
| subpartition_name | Name of the subpartition to create. Note Subpartition names must be unique across all partitions and subpartitions and follow object identifier naming conventions. |
Examples
- Prepare data.
CREATE TABLE sales_data ( order_id INT AUTO_INCREMENT, order_date DATE NOT NULL, amount DECIMAL(10,2), customer_id INT, product_id INT, quantity INT, status VARCHAR(20), PRIMARY KEY (order_id, order_date) ) PARTITION BY RANGE (TO_DAYS(order_date)) SUBPARTITION BY KEY(order_id) SUBPARTITIONS 4 ( PARTITION part_before_2022 VALUES LESS THAN (TO_DAYS('2022-01-01')), PARTITION part_2022 VALUES LESS THAN (TO_DAYS('2023-01-01')), PARTITION part_2023 VALUES LESS THAN (TO_DAYS('2024-01-01')), PARTITION part_future VALUES LESS THAN MAXVALUE ); INSERT INTO sales_data (order_date, amount, customer_id, product_id, quantity, status) VALUES ('2020-08-10', 150.00, 101, 2001, 2, 'completed'), ('2021-11-05', 250.00, 102, 2002, 1, 'pending'), ('2022-07-19', 350.00, 103, 2003, 5, 'shipped'), ('2023-04-02', 450.00, 104, 2004, 3, 'completed'); - Split a partition.
Split part_2023 into two partitions.
ALTER TABLE sales_data REORGANIZE PARTITION part_2023 INTO ( PARTITION part_2023_h1 VALUES LESS THAN (TO_DAYS('2023-07-01')), PARTITION part_2023_h2 VALUES LESS THAN (TO_DAYS('2024-01-01')) ); - Merge partitions.
Merge part_2022 and part_2023 into a single part_2022_2023 partition:
ALTER TABLE sales_data REORGANIZE PARTITION part_2022, part_2023 INTO ( PARTITION part_2022_2023 VALUES LESS THAN (TO_DAYS('2024-01-01')) ); - Modify partitions.
Reorganize all partition definitions to redefine value ranges:
ALTER TABLE sales_data REORGANIZE PARTITION part_before_2022, part_2022, part_2023, part_future INTO ( PARTITION p_2020_2021 VALUES LESS THAN (TO_DAYS('2022-01-01')), PARTITION p_2022 VALUES LESS THAN (TO_DAYS('2023-01-01')), PARTITION p_2023 VALUES LESS THAN (TO_DAYS('2024-01-01')), PARTITION p_2024 VALUES LESS THAN (TO_DAYS('2025-01-01')), PARTITION p_future VALUES LESS THAN MAXVALUE );
EXCHANGE PARTITION
Description: exchanges a partition or subpartition of a partitioned table with a non-partitioned table that has the same schema.
Description
- Executing ALTER TABLE ... EXCHANGE PARTITION swaps data between tables as follows:
- Data originally in target_partition is migrated to source_table.
- Data originally in source_table is migrated to target_partition.
- When performing an EXCHANGE PARTITION operation, ensure that source_table shares an identical schema with target_table (meaning that both tables must have identical columns, data types, engines, table attributes, and indexes).
Syntax
ALTER TABLE target_table EXCHANGE PARTITION target_partition WITH TABLE source_table [{WITH | WITHOUT} VALIDATION];
| Parameter | Description |
|---|---|
| target_table | Name of the target table for exchange. |
| target_partition | Name of the target partition for exchange. |
| source_table | Name of the source table for exchange. |
| WITHOUT VALIDATION | Skips data validation against partitioning rules and exchanges data only by updating table metadata pointers. You must ensure that data complies with partitioning rules. |
Examples
Exchange partition p2022 of partitioned table sales_data with the non-partitioned table sales_data_temp:
ALTER TABLE sales_data
EXCHANGE PARTITION p2022
WITH TABLE sales_data_temp; ANALYZE PARTITION
Description: updates statistics for a partition or subpartition.
Syntax
ALTER TABLE table_name ANALYZE PARTITION {partition_names | ALL}
Where partition_names is:
{partition_name | subpartition_name} | Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_name | Name of the partition. |
| subpartition_name | Name of the subpartition. |
Examples
- Analyze partition part_2022 of the sales_data table:
ALTER TABLE sales_data ANALYZE PARTITION part_2022;
- Analyze subpartition part_2022_sp1 of the sales_data table:
ALTER TABLE sales_data ANALYZE PARTITION part_2022_sp1;
CHECK PARTITION
Description: checks a partition or subpartition and indicates whether its data or indexes are corrupted.
Syntax
ALTER TABLE table_name CHECK PARTITION {partition_names | ALL}
Where partition_names is:
{partition_name | subpartition_name} | Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_name | Name of the partition. |
| subpartition_name | Name of the subpartition. |
Examples
- Check partition part_2022 of the sales_data table:
ALTER TABLE sales_data CHECK PARTITION part_2022;
- Check subpartition part_2022_sp1 of the sales_data table:
ALTER TABLE sales_data CHECK PARTITION part_2022_sp1;
OPTIMIZE PARTITION
Description
If a large number of rows are deleted from a partition or subpartition, or if updates are made to variable-length rows (containing VARCHAR, BLOB, or TEXT columns), run ALTER TABLE ... OPTIMIZE PARTITION to optimize the partition or subpartition, reclaim unused space, and defragment partition data files.
Syntax
ALTER TABLE table_name OPTIMIZE PARTITION {partition_names | ALL}
Where partition_names is:
{partition_name | subpartition_name} | Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_name | Name of the partition. |
| subpartition_name | Name of the subpartition. |
Examples
- Optimize partition part_2022 of the sales_data table:
ALTER TABLE sales_data OPTIMIZE PARTITION part_2022;
- Optimize subpartition part_2022_sp1 of the sales_data table:
ALTER TABLE sales_data OPTIMIZE PARTITION part_2022_sp1;
REBUILD PARTITION
Description: rebuilds a partition.
Syntax
ALTER TABLE table_name REBUILD PARTITION {partition_names | ALL}
| Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_name | Name of the partition. |
Examples
Rebuild partition part_2022 of the sales_data table:
ALTER TABLE part_range_list REBUILD PARTITION part_2022;
REPAIR PARTITION
Description: repairs a corrupted partition or subpartition.
Syntax
ALTER TABLE table_name REPAIR PARTITION {partition_names | ALL}
Where partition_names is:
{partition_name | subpartition_name} | Parameter | Description |
|---|---|
| table_name | Name of the partitioned table (schema-qualified names are supported). |
| partition_name | Name of the partition. |
| subpartition_name | Name of the subpartition. |
Examples
- Repair partition part_2022 of the sales_data table:
ALTER TABLE sales_data REPAIR PARTITION part_2022;
- Repair subpartition part_2022_sp1 of the sales_data table:
ALTER TABLE sales_data REPAIR PARTITION part_2022_sp1;
REMOVE PARTITION
Description: removes partition and subpartition structures from a partitioned table.
Syntax
ALTER TABLE ... REMOVE PARTITIONING removes partition and subpartition structures from a partitioned table and converts it into a non-partitioned table without data loss.
ALTER TABLE table_name REMOVE PARTITIONING Examples
Remove all partitioning structures from the sales_data table:
ALTER TABLE sales_data REMOVE PARTITIONING;
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot