1. Instructions for use:
1. After this script is used as a partition, partitions are automatically added at regular intervals. (For tables that are automatically partitioned, you must manually divide the partitions first)
2. Every 15 days, the timer executes a stored procedure and adds 15 new partitions on the last day of the partition date.
3. In the Script, Auto_partitions. SQL is the stored procedure.
4. In the Script, Timer_event. SQL is the scheduled event Script.
5. MySQL5.5 the EVENT mechanism is not enabled by default. You need to add [mysqld] event_schedld = ON in the my. cnf File
7. Add and open the file online. This is very important. open_files_limit = 5000
Ii. Partition script
- DELIMITER |
- DROP PROCEDUREIf exists create_Partition |
- CREATE PROCEDURECreate_Partition (INDatabaseNameVARCHAR(50 ),INTableNameVARCHAR(50 ))
- Rochelle end:BEGIN
- DECLAREMAX_PARTITION_DESCRIPTIONVARCHAR(255)DEFAULT0;
- DECLAREP_NAMEVARCHAR(255)DEFAULT0;
- DECLAREP_DESCRIPTIONVARCHAR(255)DEFAULT0;
- DECLAREIINT DEFAULT1;
- DECLAREISEXIST_PARTITIONVARCHAR(255)DEFAULT0;
- SELECTPARTITION_NAMEINTOISEXIST_PARTITIONFROMInformation_schema.PARTITIONSWHERETABLE_SCHEMA = databaseNameANDTABLE_NAME = tableName LIMIT 1;
- IF ISEXIST_PARTITION <=>"" THEN
- SELECT "Partition table not is exist" AS "****** ERROR *****";
- LEAVE L_END;
- ENDIF;
- SELECTPartition_descriptionINTOMAX_PARTITION_DESCRIPTIONFROMInformation_schema.PARTITIONSWHERETABLE_SCHEMA = databaseNameANDTABLE_NAME = tableNameORDER BYPartition_descriptionDESCLIMIT 1;
- IF MAX_PARTITION_DESCRIPTION <=>"" THEN
- SELECT "Partition table is error" AS "****** ERROR *****";
- LEAVE L_END;
- ENDIF;
- SETMAX_PARTITION_DESCRIPTION =REPLACE(MAX_PARTITION_DESCRIPTION,'\'','');
- WHILE I <= 15 DO
- SETP_DESCRIPTION = adddate (MAX_PARTITION_DESCRIPTION, INTERVAL IDay);
- SETP_NAME =REPLACE(P_DESCRIPTION,'-','');
- SET@ S = CONCAT ('Alter table', TableName,'Add PARTITION (PARTITION P', P_NAME,'Values less (\'', P_DESCRIPTION,'\'))');
- SELECT@ S;
- PREPAREStmt2FROM@ S;
- EXECUTEStmt2;
- DEALLOCATE PREPAREStmt2;
- SETI = I + 1;
- ENDWHILE;
- ENDRochelle end; |
- DELIMITER;
# The input parameter databaseName is the database name and the tableName is the table name.
3. Add event processing
- DELIMITER |
- CREATEEVENT auto_set_partitions
- ONSCHEDULE
- EVERY 15DAY
- DO
- BEGIN
- CALL create_Partition ('Database _ name','Table _ name');
- /* If you want to partition multiple tables, you can write multiple CALL calls.
- CALL create_Partition ('Database _ name','Table _ name');
- */
- END|
- DELIMITER;
This event is executed every 15 days.