1、python生成分区表,如下:
#coding=gb18030
from datetime import *
class GeneMysqlPartition:
def Gene(self):
pFile = open('MysqlPartition.txt','w')
i=0
curDate=date.today()-timedelta(days=10)
while(i<10):
ss= "partition p%04d%02d%02d values less than (to_days('%s-%s-%s')),\n" %(curDate.year,curDate.month,curDate.day,curDate.year,curDate.month,curDate.day)
pFile.write(ss)
i=i+1
curDate=curDate+timedelta(days=1)
pFile.close()
if __name__ == '__main__':
geneA = GeneMysqlPartition();
geneA.Gene();
2、创建表,如下:
use niu_test;
create table alarm_log(ID int, MSG varchar(32), TIME datetime)
engine=innodb
partition by range(to_days(TIME))
(
partition p20151220 values less than (to_days('2015-12-20'))
);
3、动态管理分区表
DROP PROCEDURE IF EXISTS `exe_dynamic_sql`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `exe_dynamic_sql`(iDynamicSql TEXT)
BEGIN
set @exeSql = iDynamicSql;
prepare stmt from @exeSql;
execute stmt;
deallocate prepare stmt; -- 释放掉预处理段
END
;;
DELIMITER ;
DROP PROCEDURE IF EXISTS `manage_partitions`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `manage_partitions`(iDataBaseName varchar(100), iTableName varchar(100), iOldPartitionSaveDays int, iPartitionPreserverDays int, iPartitionIntervalDays int)
BEGIN
declare vPartitionName varchar(32) default '';
declare vIsOver int default 0;
declare vCurDate date default curdate();
declare vOldDate date default curdate();
declare vLastDate date default curdate();
declare vExpectDate date default curdate();
declare vLastPartitionName varchar(32) default '';
declare vDynamicSql text default '';
declare vCursorPartitionName cursor for select partition_name from information_schema.partitions where table_schema=iDataBaseName and table_name=iTableName;
declare continue handler for sqlstate '02000' set vIsOver = 1;
-- delete old partition
open vCursorPartitionName;
fetch vCursorPartitionName into vPartitionName;
while(vIsOver = 0) do
set vOldDate = substr(vPartitionName,2);
-- select datediff(vCurDate,vOldDate);
if(datediff(vCurDate,vOldDate)> iOldPartitionSaveDays) then
set vDynamicSql = concat('alter table ',iTableName,' drop partition ',vPartitionName);
-- select vDynamicSql;
call exe_dynamic_sql(vDynamicSql);
end if;
fetch vCursorPartitionName into vPartitionName;
end while;
close vCursorPartitionName;
-- delete old partition
-- add new partition
set vLastPartitionName = (select max(partition_name) from information_schema.partitions where table_schema=iDataBaseName and table_name=iTableName);
set vLastDate = substr(vLastPartitionName,2);
select date_add(curdate(),interval iPartitionPreserverDays day) into vExpectDate;
while(vLastDate < vExpectDate) do
set vLastDate = vLastDate + interval iPartitionIntervalDays day;
set vDynamicSql = concat('alter table ',iTableName,' add partition (partition p',date_format(vLastDate,'%Y'),date_format(vLastDate,'%m'),date_format(vLastDate,'%d'),' values less than (to_days(\'', vLastDate,'\')))');
-- select vDynamicSql;
call exe_dynamic_sql(vDynamicSql);
end while;
-- add new partition
END
;;
DELIMITER ;
4、计划任务
DROP EVENT IF EXISTS `event_day`;
DELIMITER ;;
CREATE EVENT `event_day` ON SCHEDULE EVERY 1 DAY STARTS '2015-06-20 08:05:00' ENDS '2025-06-20 08:00:00' ON COMPLETION PRESERVE ENABLE DO begin
call manage_partitions('niu_test','alarm_log',365*2,30,7);
call manage_partitions('niu_test','realdata_log',365*2,30,7);
end
;;
DELIMITER ;