MySQL regularly adds partitions

        After MySQL creates partitions, sometimes it is necessary to automatically create partitions. For example, the amount of data in some tables is very large, and some data is hot data. After partitioning by date, the business volume is still going, and it will take a while to create partitions again. As a programmer, I can't bear to create it manually.

        First create a stored procedure that can automatically add partitions (if you need to automatically delete partitions, this article is also applicable)

DELIMITER $$
USE `me`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_schedule_log_partition`()
BEGIN
set @pname = concat('p',date_format(curdate(),'%Y%m%d'));
set @nexttodays = to_days(date_add(curdate(), INTERVAL 1 DAY));
set @executeStr = concat('alter table s_schedule_log add partition (PARTITION ',  @pname, ' VALUES LESS THAN (', @nexttodays, ' ))');
prepare stmt from @executeStr;
execute stmt;
deallocate prepare stmt;
select @executeStr;
END$$

DELIMITER ;
;

        The stored procedure can be called manually, is there any problem. Then write a timed task

DELIMITER ||
CREATE EVENT add_schedule_log_partition_event
          ON SCHEDULE
          EVERY 1 day STARTS '2022-05-15 01:00:00'
          DO
      BEGIN
          call `me`.`add_schedule_log_partition`;
 END ||
DELIMITER ;

Note: The scheduled task requires the parameter event_scheduler to be enabled, use sql: show variables like '%schedule%' to view the status.

 Enable task status: set global event_scheduler = 'ON'

Guess you like

Origin blog.csdn.net/u011471105/article/details/124778370