CREATE TABLE `test` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘主键’, `datetime` int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘时间’, PRIMARY KEY (`id`,`datetime`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘测试表分区’/*!50100 PARTITION BY RANGE (datetime)(PARTITION p201511 VALUES LESS THAN (UNIX_TIMESTAMP(“2015-12-01 00:00:00”)) ENGINE = InnoDB, PARTITION p201512 VALUES LESS THAN (UNIX_TIMESTAMP(“2016-01-01 00:00:00”)) ENGINE = InnoDB) */;
2、创建存储过程,用于下面MYSQL的event定时器调用,自动创建就是event来完成的。这段存储过程的内容就是创建下月分区的意思。
DELIMITER $$
#该表所在数据库名称USE `test`$$DROP PROCEDURE IF EXISTS `create_partition_by_month`$$CREATE PROCEDURE `create_partition_by_month`(IN_SCHEMANAME VARCHAR(64), IN_TABLENAME VARCHAR(64))BEGIN DECLARE ROWS_CNT INT UNSIGNED; DECLARE BEGINTIME TIMESTAMP; DECLARE ENDTIME INT UNSIGNED; DECLARE PARTITIONNAME VARCHAR(16); DECLARE ENDTIME_DATETIME VARCHAR(30); SET BEGINTIME = DATE(NOW() – INTERVAL DAY(NOW()) DAY + INTERVAL 1 DAY + INTERVAL 1 MONTH); SET PARTITIONNAME = DATE_FORMAT( BEGINTIME, ‘p%Y%m’ ); SET ENDTIME = UNIX_TIMESTAMP(BEGINTIME + INTERVAL 1 MONTH); SET ENDTIME_DATETIME = FROM_UNIXTIME(ENDTIME); SELECT COUNT(*) INTO ROWS_CNT FROM information_schema.partitions WHERE table_schema = IN_SCHEMANAME AND table_name = IN_TABLENAME AND partition_name = PARTITIONNAME; IF ROWS_CNT = 0 THEN
SET @SQL = CONCAT( ‘ALTER TABLE `’, IN_SCHEMANAME, ‘`.`’, IN_TABLENAME, ‘`’, ‘ ADD PARTITION (PARTITION ‘, PARTITIONNAME, ” VALUES LESS THAN (UNIX_TIMESTAMP(‘”,
ENDTIME_DATETIME ,“‘)) ENGINE = InnoDB);” );
PREPARE STMT FROM @SQL; EXECUTE STMT; DEALLOCATE PREPARE STMT; ELSE SELECT CONCAT(“partition `”, PARTITIONNAME, “` for table `”,IN_SCHEMANAME, “.”, IN_TABLENAME, “` already exists”) AS result; END IF;END$$DELIMITER ;
show procedure status where db=’logtop’;
DELIMITER $$
#该表所在的数据库名称USE `test`$$CREATE EVENT IF NOT EXISTS `e_part_manage`ON SCHEDULE EVERY 1 MINUTE #执行周期,还有天、月等等STARTS ‘2016-01-07 18:27:00’ON COMPLETION PRESERVEENABLECOMMENT ‘Creating partitions’DO BEGIN
#调用刚才创建的存储过程,第一个参数是数据库名称,第二个参数是表名称 CALL test.create_partition_by_month(‘test’,‘test’);END$$DELIMITER ;
3.1查看已经创建的event
SELECT * FROM information_schema.EVENTS;
vim /etc/my.cnf
[mysqld]
event_scheduler=ON
5、插入测试数据
INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-10”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-11”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-12”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-13”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-13”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-14”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-15”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-16”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-17”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-18”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-19”));INSERT INTO `test`.`test` (`datetime`) VALUES (unix_timestamp(“2015-12-20”));
SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME=‘test’;
#添加分区
ALTER TABLE `test`.`test` ADD PARTITION (PARTITION p201601 VALUES LESS THAN (UNIX_TIMESTAMP(“2016-02-01 00:00:00”)) ENGINE = InnoDB);
#删除分区ALTER TABLE test.testDROP PARTITION p201511;
查看查询SQL是否使用的分区
explain PARTITIONS select *,FROM_UNIXTIME(datetime) from test where datetime > UNIX_TIMESTAMP(“2015-12-10”) and datetime < UNIX_TIMESTAMP(“2015-12-15”);