Automating MySQL Partitioning

Aryan01
3 min readSep 14, 2023

--

After learning how to delete records from large tables the next requirement was to auto-maintain the partitions of the table.

Automated partition maintenance refers to the process of dynamically creating new partitions and removing outdated ones from a database table. This ensures that data is efficiently organized, with unnecessary partitions being dropped as they become obsolete.

After extensive research, I found no readily available tools tailored for this purpose.
Consequently, I took the initiative to develop a MySQL script named
Partition Keeper.” This script is executed via an event, allowing it to autonomously manage MySQL partitions. This approach streamlines the maintenance process, enhancing database performance and efficiency.

The Partitioning Strategy

Partitioning involves several key steps. I’ll break them down for you:

Step 1: Creating Sample Table

CREATE TABLE application (
id INT AUTO_INCREMENT PRIMARY KEY,
CreatedAt DATETIME,
-- Add other columns as needed
) PARTITION BY RANGE (YEAR(CreatedAt)) (
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (1991),
PARTITION p2 VALUES LESS THAN (1992),
PARTITION p3 VALUES LESS THAN (1993),
PARTITION p4 VALUES LESS THAN (1994),
PARTITION p5 VALUES LESS THAN MAXVALUE
);

Step 2: Setting Up Automatic Maintenance

We begin by creating an event that triggers the partition maintenance process at specified intervals. In this example, the event is scheduled to run every day.

DELIMITER $$
CREATE EVENT daily_perform_partition_maintenance_event
ON SCHEDULE EVERY 1 DAY STARTS NOW()
DO
CALL perform_partition_maintenance('db_name', 'application', 1, 3, 5);
$$
DELIMITER ;

Step 3: Creating New Partitions

Next, we define a procedure to create new partitions.


DROP PROCEDURE IF EXISTS create_new_partitions;
DELIMITER $$
CREATE PROCEDURE create_new_partitions(p_schema varchar(64), p_table varchar(64), p_months_to_add int)
BEGIN
DECLARE current_date DATETIME;
SET current_date = NOW();

DECLARE new_partition_start DATETIME;
DECLARE new_partition_end DATETIME;

SET new_partition_start = ADDDATE(current_date, INTERVAL p_months_to_add MONTH);
SET new_partition_end = ADDDATE(new_partition_start, INTERVAL 1 YEAR);

SET @sql = CONCAT(
'ALTER TABLE `', p_schema, '`.`', p_table, '` ',
'ADD PARTITION (',
'PARTITION p', YEAR(new_partition_start), ' ',
'VALUES LESS THAN (', YEAR(new_partition_end), ')'
')'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
$$
DELIMITER ;

Step 4: Procedure to drop old partitions
To keep the dataset manageable, we also need to remove old partitions. This procedure identifies and drops partitions older than a specified threshold.

DROP PROCEDURE IF EXISTS drop_old_partitions;
DELIMITER $$
CREATE PROCEDURE drop_old_partitions(p_schema varchar(64), p_table varchar(64), p_months_to_keep int)
BEGIN
DECLARE cutoff_date DATETIME;
SET cutoff_date = ADDDATE(NOW(), INTERVAL -p_months_to_keep MONTH);

SET @sql = CONCAT(
'ALTER TABLE `', p_schema, '`.`', p_table, '` ',
'DROP PARTITION p', YEAR(cutoff_date)
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
$$
DELIMITER ;

Step 5: Coordinating Maintenance

Finally, we create a high-level procedure that orchestrates partition maintenance. It calls the procedures we defined earlier with specific parameters.

DELIMITER $$
CREATE PROCEDURE perform_partition_maintenance(p_schema varchar(64), p_table varchar(64), p_months_to_add int, p_months_to_keep int, p_seconds_to_sleep int)
BEGIN
CALL drop_old_partitions(p_schema, p_table, p_months_to_keep, p_seconds_to_sleep);
CALL create_new_partitions(p_schema, p_table, p_months_to_add);
END;
$$
DELIMITER ;

Real-World Application

Let’s consider an example where we have a table named `application` with partitions based on `CreatedAt`. New partitions are added as time progresses, and old partitions are dropped to keep the dataset current.

Benefits of Automatic Partitioning

Automating partition maintenance offers several advantages:

  1. Efficient Data Management: Automatic partitioning ensures that the table is organized optimally for performance and maintenance.
  2. Improved Query Performance: Smaller partitions mean that queries can be executed more quickly, as the database only needs to search a subset of the data.
  3. Simplified Maintenance: Old partitions are automatically removed, reducing the manual effort required for maintenance tasks.
  4. Scalability: As data continues to grow, the partitioning strategy can adapt to handle larger volumes.

Conclusion

Automating partition maintenance for a MySQL table is a crucial step in ensuring optimal performance and efficient data management. By following the steps outlined in this guide, you can streamline the process and reap the benefits of a well-organized database.

Remember to adapt the code snippets to your specific database schema and requirements. With automated partitioning in place, you’ll be better equipped to handle large datasets effectively.

--

--