时间:2021-05-14 10:37:32 | 栏目:Mysql | 点击:次
前言
在最近的项目中,我们需要保存大量的数据,而且这些数据是有有效期的,为了提供查询效率以及快速删除过期数据,我们选择了MySQL的分区机制。把数据按照时间进行分区。
分区类型
分区命令
创建分区
CREATE TABLE `access_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `access_time` datetime NOT NULL, PRIMARY KEY (`id`,`access_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (to_days(access_time)) (PARTITION p1 VALUES LESS THAN (to_days(20190101)) ENGINE = InnoDB, PARTITION p2 VALUES LESS THAN (to_days(20190102)) ENGINE = InnoDB, PARTITION p3 VALUES LESS THAN (to_days(20190103)) ENGINE = InnoDB) */;
创建后可以看到,每个分区都会对应1个ibd文件
分区表
新增分区
alter table access_log add partition( partition p4 values less than (to_days('20190105')) );
删除分区
alter table access_log drop partition p1;
拆分分区
alter table access_log reorganize partition p4 into( -> partition s0 values less than(to_days('20190104')), -> partition s1 values less than(to_days('20190105')) -> );
合并分区
alter table access_log reorganize partition s0,s1 into ( partition p4 values less than (to_days('20190105')) );
注意事项
常见问题
alter table access_log partition by range(to_days(access_time))( partition p1 values less than (to_days('20191202')), partition p2 values less than (to_days('20191203')), partition po values less than (maxvalue) )
参考
总结