在2.0中,history、history_str、history_uint、trends、trends_uint、events这几张表大家应该比较熟悉,大量的历史数据都存在这几张表中,我们现在就对这几张表做分区:
1、分别create
以history为例:
CREATE TABLE `history` (`itemid` bigint(20) unsigned NOT NULL,`clock` int(11) NOT NULL DEFAULT '0',`value` double(16,4) NOT NULL DEFAULT '0.0000',`ns` int(11) NOT NULL DEFAULT '0',KEY `history_1` (`itemid`,`clock`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 partition by range (clock) (partition p201307 values less than (unix_timestamp("2013-07-01 00:00:00")),partition p201308 values less than (unix_timestamp("2013-08-01 00:00:00"))); 1.1 alter已有的表
alter table history_uint partition by RANGE (clock) (partition p201306 values less than (unix_timestamp("2013-07-01")),partition p201307 values less than (unix_timestamp("2013-08-01")),partition p201308 values less than (unix_timestamp("2013-09-01"))); 2、发现分区已经超时了,需要添加
以history为例:
alter table history add partition (partition p201309 values less than (unix_timestamp("2013-10-01 00:00:00"))); 这么做之后,你会发现当你查看近一个月的数据的时候,数据出来的比较快,同时有可能之前的图像断点问题也解决了。然后写脚本定期的去删除分区,同样可以做到删除垃圾数据的效果。