MySQL Auto partition

Source: Internet
Author: User

Automatic partitioning requires the event scheduler in MySQL to be turned on, and you can see if the scheduler is turned on by the following command

 like ' %scheduler% ';

If not, open it with the following command

SET = 1;

1. Create a partitioned table

CREATE TABLESales (IDINTauto_increment, AmountDOUBLE  not NULL, CreatetimeDATETIME  not NULL,    PRIMARY KEY(ID, createtime)) ENGINE=innodbpartition byRANGE ( Year(Createtime)) (PARTITION p_2010VALUESLess THAN ( .), #2011年以前一个分区 PARTITION p_2011VALUESLess THAN ( -) # .-20,121 partitions)

To view the current table partition information:

   SELECT partition_name,partition_description,partition_method,table_rows,create_time     from information_schema. Partitions   WHERE='demo'and= ' Sales ' ORDER  by DESC ;

Create a new stored procedure to add a partition to a partitioned table

DROP PROCEDURE IF EXISTScreate_partition;CREATE PROCEDURECreate_partition (inchDatabaseNameVARCHAR( -),inchTableNameVARCHAR( -),inchPartitionnameprefixVARCHAR( -)) L_end:BEGIN     DECLAREMax_partition_descriptionVARCHAR(255)DEFAULT ""; DECLAREP_nameVARCHAR(255)DEFAULT ""; DECLAREP_descriptionVARCHAR(255)DEFAULT ""; DECLAREIINT DEFAULT 1; DECLAREIsexist_partitionVARCHAR(255)DEFAULT ""; DECLAREPartition_countINT DEFAULT 1; #一次性增加一个分区数SELECTIfnull (Partition_name, "") intoIsexist_partition frominformation_schema. PartitionsWHERETable_schema=DatabaseName andtable_name=TableName LIMIT1 ; IFIsexist_partition<=>"" Then       SELECT"PartitionTable  not  isExist as"*****ERROR*****";     LEAVE L_end; END IF; SELECTIfnull (Partition_description, "") intoMax_partition_description frominformation_schema. PartitionsWHERETable_schema=DatabaseName andtable_name=TableNameORDER  byPartition_descriptionDESCLIMIT1; IFMax_partition_description<=>"" Then       SELECT"PartitionTable  isError as"*****ERROR*****";     LEAVE L_end; END IF; SETMax_partition_description= REPLACE(Max_partition_description,'\"', "');           While I <= partition_count do SET p_description = max_partition_description+i; SET p_name = REPLACE (p_description,'-', "'); SET @s=concat ('ALTER TABLE ', TableName,' ADDPARTITION (PARTITION', Partitionnameprefix,p_name,' VALUESLess THAN (', P_description,'))');           SELECT @S;           PREPARE stmt2 from @S;           EXECUTE stmt2;           Deallocate PREPARE stmt2;    SET i = i + 1;          END while; END L_end;

Create an event timer to create a partition on a timed basis

DELIMITER $$CREATEEVENT auto_set_partitions onSCHEDULE every5MINUTE COMMENT'Auto set partitions for table' DoBEGINCall create_partition ('MySQL','Test','P'); END$$

This will create a new partition on the Sales partition table every 5 minutes.

MySQL Auto partition

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.