Reorganize Partitions with ALTER TABLE
In this step, we will learn how to reorganize partitions using the ALTER TABLE
statement in MySQL. Reorganizing partitions can be useful for various reasons, such as merging partitions, splitting partitions, or adding new partitions.
First, ensure you are connected to the MySQL server and using the sales_data
database. If you closed the connection in the previous step, reconnect using:
mysql -u root -p
Enter the root password if prompted. Then, select the sales_data
database:
USE sales_data;
Let's start by adding a new partition for the year 2024. Currently, all sales data from 2024 onwards are stored in the pFuture
partition. We can create a specific partition for 2024 using the ALTER TABLE
statement with the ADD PARTITION
clause:
ALTER TABLE sales ADD PARTITION (PARTITION p2024 VALUES LESS THAN (2025));
This statement adds a new partition named p2024
that will contain all rows where the year of sale_date
is less than 2025 (i.e., year 2024). After adding the partition, we need to reorganize the pFuture
partition to exclude the year 2024.
ALTER TABLE sales REORGANIZE PARTITION pFuture INTO (
PARTITION p2025 VALUES LESS THAN (2026),
PARTITION pFuture VALUES LESS THAN MAXVALUE
);
This command splits the pFuture
partition into p2025
and a new pFuture
partition. p2025
will contain data for the year 2025, and the new pFuture
will contain data for 2026 and beyond.
Now, let's verify the partition structure:
SELECT
PARTITION_NAME,
TABLE_ROWS
FROM
INFORMATION_SCHEMA.PARTITIONS
WHERE
TABLE_SCHEMA = 'sales_data' AND TABLE_NAME = 'sales';
You should see the new p2024
, p2025
and updated pFuture
partitions in the output.
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p2020 | 1 |
| p2021 | 2 |
| p2022 | 2 |
| p2023 | 2 |
| p2024 | 1 |
| p2025 | 0 |
| pFuture | 0 |
+----------------+------------+
Next, let's see how to merge partitions. Suppose we want to merge the p2020
and p2021
partitions into a single partition named p2020_2021
. We can do this using the ALTER TABLE
statement with the REORGANIZE PARTITION
clause:
ALTER TABLE sales REORGANIZE PARTITION p2020, p2021 INTO (
PARTITION p2020_2021 VALUES LESS THAN (2022)
);
This statement merges the p2020
and p2021
partitions into a new partition named p2020_2021
. The VALUES LESS THAN (2022)
clause specifies that this partition will contain all rows where the year of sale_date
is less than 2022.
Now, let's verify the partition structure again:
SELECT
PARTITION_NAME,
TABLE_ROWS
FROM
INFORMATION_SCHEMA.PARTITIONS
WHERE
TABLE_SCHEMA = 'sales_data' AND TABLE_NAME = 'sales';
You should see that the p2020
and p2021
partitions have been replaced by the p2020_2021
partition.
+------------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+------------------+------------+
| p2020_2021 | 3 |
| p2022 | 2 |
| p2023 | 2 |
| p2024 | 1 |
| p2025 | 0 |
| pFuture | 0 |
+------------------+------------+
Finally, let's see how to drop a partition. Suppose we want to remove the p2025
partition. We can do this using the ALTER TABLE
statement with the DROP PARTITION
clause:
ALTER TABLE sales DROP PARTITION p2025;
Now, let's verify the partition structure one last time:
SELECT
PARTITION_NAME,
TABLE_ROWS
FROM
INFORMATION_SCHEMA.PARTITIONS
WHERE
TABLE_SCHEMA = 'sales_data' AND TABLE_NAME = 'sales';
You should see that the p2025
partition has been removed.
+------------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+------------------+------------+
| p2020_2021 | 3 |
| p2022 | 2 |
| p2023 | 2 |
| p2024 | 1 |
| pFuture | 0 |
+------------------+------------+
By using the ALTER TABLE
statement with various clauses, you can easily reorganize your partitions to meet your changing data management needs.