标签:
1 开启event_scheduler
1.1 找到my.cnf的位置
find / |grep my.cnf
1.2 修改my.cnf配置
vim ./usr/my.cnf
[mysqld]
event_scheduler=ON
1.3 restart mysql
service mysql restart.
2 创建分区表
1.1 创建分区表
DROP TABLE IF EXISTS `tb_report`;
CREATE TABLE IF NOT EXISTS `tb_report` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`gen_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP comment "for partition used",
`time` datetime NOT NULL DEFAULT ‘1972-01-01 00:00:00‘ comment "for service used",
`dir` tinyint(1) NOT NULL DEFAULT ‘0‘,
`domain_id` int(10) not null default 0,
`accept` int(10) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘TOTAL = accept + reject‘,
`reject` int(10) unsigned NOT NULL DEFAULT ‘0‘ ,
PRIMARY KEY (id,gen_time),--partion column must in key
INDEX (domain_id)
) ENGINE=innodb DEFAULT CHARSET=utf8
PARTITION BY RANGE(TO_DAYS(gen_time))
(
PARTITION pbasic VALUES LESS THAN (0)
);
1.2 定时创建新分区和删除过期分区
delimiter $$
DROP EVENT if exists `event_auto_partition`;$$
CREATE EVENT `event_auto_partition` ON SCHEDULE EVERY 1 day STARTS ‘1972-01-01 00:00:00‘ ON COMPLETION PRESERVE DO
begin
call create_partition_per_day(DATE_ADD(now(), interval 0 month),‘tb_report‘);
call create_partition_per_day(DATE_ADD(now(), interval 1 month),‘tb_report‘);
call drop_partition_by_month(DATE_ADD(now(), interval -3 month),‘tb_report‘);
end;
$$
ps:create_partition_per_day and drop_partition_by_month need your implementation。
1.3 对于分区表,MySQL不支持命令CHECK TABLE,OPTIMIZE TABLE,ANALYZE TABLE,或REPAIR TABLE
那么当一个分区表经过多次改变或分区中删除了大量的行留下很多碎片时怎么解决呢?msyql 有个代替方案,ALTER TABLE ... OPTIMIZE PARTITION。
ALTER TABLE t1 OPTIMIZE PARTITION p;
在一个给定的分区表上使用“OPTIMIZE PARTITION”等同于在那个分区上运行CHECK PARTITION,ANALYZE PARTITION,和REPAIR PARTITION。
这是个低效的做法,最好的做法是按照日期直接删除不再使用的partition。
标签:
原文地址:http://www.cnblogs.com/dogharry/p/4306881.html