There is a table tb_3a_huandan_detail, which has about 300W of data every day. The query is too slow. I learned about it online and can be used as a table partition. Because the data is large, I decided to do a scheduled task and automatically partition it every day.
1. Before automatically adding partitions, you must manually divide the table into several areas.
ALTER TABLE tb_3a_huandan_detail PARTITION BY RANGE (TO_DAYS(ServiceStartTime)) ( PARTITION p20160523 VALUES LESS THAN (TO_DAYS('2016-05-23')), PARTITION p20160524 VALUES LESS THAN (TO_DAYS('2016-05-24')), PARTITION p20160525 VALUES LESS THAN (TO_DAYS('2016-05-25')), PARTITION p20160526 VALUES LESS THAN (TO_DAYS('2016-05-26')), PARTITION p20160527 VALUES LESS THAN (TO_DAYS('2016-05-27')) )
2. The partition has been saved as follows:
DELIMITER $$ USE `nres`$$ DROP PROCEDURE IF EXISTS `create_Partition_3Ahuadan`$$ CREATE DEFINER=`nres`@`%` PROCEDURE `create_Partition_3Ahuadan`() BEGIN /* Transaction rollback, in fact, it has no effect here. ALTER TABLE is implicitly submitted and cannot be rolled back. */ DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK; START TRANSACTION; /* Check the maximum partition of this table in the system table and get the date of the largest partition. When creating a partition, the name is stored in date format, which is convenient for later maintenance */ SELECT REPLACE(partition_name,'p','') INTO @P12_Name FROM INFORMATION_SCHEMA.PARTITIONS WHERE table_name='tb_3a_huandan_detail' ORDER BY partition_ordinal_position DESC LIMIT 1; SET @Max_date= DATE(DATE_ADD(@P12_Name+0, INTERVAL 1 DAY))+0; /* Modify the table, add one partition behind the largest partition, and add 1 day to the time range */ SET @s1=CONCAT('ALTER TABLE tb_3a_huandan_detail ADD PARTITION (PARTITION p',@Max_date,' VALUES LESS THAN (TO_DAYS (''',DATE(@Max_date),''')))'); /* Output view add partition statement*/ SELECT @s1; PREPARE stmt2 FROM @s1; EXECUTE stmt2; DEALLOCATE PREPARE stmt2; /* Take out the name of the smallest partition and delete it. Note: Deleting a partition will delete data in the partition at the same time, be careful */ /*select partition_name into @P0_Name from INFORMATION_SCHEMA.PARTITIONS where table_name='tb_3a_huandan_detail' order by partition_ordinal_position limit 1; SET @s=concat('ALTER TABLE tb_3a_huandan_detail DROP PARTITION ',@P0_Name); PREPARE stmt1 FROM @s; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; */ /* submit */ COMMIT ; END$$ DELIMITER ;
3. Add timed events
DELIMITER || CREATE EVENT Partition_3Ahuadan_event ON SCHEDULE EVERY 1 day STARTS '2016-05-27 23:59:59' DO BEGIN CALL nres.`create_Partition_3Ahuadan`; END || DELIMITER ;
refer to:
/m582445672/article/details/7670743
/
This is the end of this article about the implementation of MySQL's automatic partition addition every day. For more related content on MySQL's automatic partition addition, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!