MySQL5.5 automatic partition script

Source: Internet
Author: User

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

  1. DELIMITER |
  2. DROP PROCEDUREIf exists create_Partition |
  3. CREATE PROCEDURECreate_Partition (INDatabaseNameVARCHAR(50 ),INTableNameVARCHAR(50 ))
  4. Rochelle end:BEGIN
  5. DECLAREMAX_PARTITION_DESCRIPTIONVARCHAR(255)DEFAULT0;
  6. DECLAREP_NAMEVARCHAR(255)DEFAULT0;
  7. DECLAREP_DESCRIPTIONVARCHAR(255)DEFAULT0;
  8. DECLAREIINT DEFAULT1;
  9. DECLAREISEXIST_PARTITIONVARCHAR(255)DEFAULT0;
  10. SELECTPARTITION_NAMEINTOISEXIST_PARTITIONFROMInformation_schema.PARTITIONSWHERETABLE_SCHEMA = databaseNameANDTABLE_NAME = tableName LIMIT 1;
  11. IF ISEXIST_PARTITION <=>"" THEN
  12. SELECT "Partition table not is exist" AS "****** ERROR *****";
  13. LEAVE L_END;
  14. ENDIF;
  15. SELECTPartition_descriptionINTOMAX_PARTITION_DESCRIPTIONFROMInformation_schema.PARTITIONSWHERETABLE_SCHEMA = databaseNameANDTABLE_NAME = tableNameORDER BYPartition_descriptionDESCLIMIT 1;
  16. IF MAX_PARTITION_DESCRIPTION <=>"" THEN
  17. SELECT "Partition table is error" AS "****** ERROR *****";
  18. LEAVE L_END;
  19. ENDIF;
  20. SETMAX_PARTITION_DESCRIPTION =REPLACE(MAX_PARTITION_DESCRIPTION,'\'','');
  21. WHILE I <= 15 DO
  22. SETP_DESCRIPTION = adddate (MAX_PARTITION_DESCRIPTION, INTERVAL IDay);
  23. SETP_NAME =REPLACE(P_DESCRIPTION,'-','');
  24. SET@ S = CONCAT ('Alter table', TableName,'Add PARTITION (PARTITION P', P_NAME,'Values less (\'', P_DESCRIPTION,'\'))');
  25. SELECT@ S;
  26. PREPAREStmt2FROM@ S;
  27. EXECUTEStmt2;
  28. DEALLOCATE PREPAREStmt2;
  29. SETI = I + 1;
  30. ENDWHILE;
  31. ENDRochelle end; |
  32. DELIMITER;

# The input parameter databaseName is the database name and the tableName is the table name.

3. Add event processing

  1. DELIMITER |
  2. CREATEEVENT auto_set_partitions
  3. ONSCHEDULE
  4. EVERY 15DAY
  5. DO
  6. BEGIN
  7. CALL create_Partition ('Database _ name','Table _ name');
  8. /* If you want to partition multiple tables, you can write multiple CALL calls.
  9. CALL create_Partition ('Database _ name','Table _ name');
  10. */
  11.  END|
  12. DELIMITER;

This event is executed every 15 days.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.