日志数据需要按时间进行搜寻,因此需要按日志时间重建分区。
1.创建log2,按时间分区(每月1个分区)
CREATE TABLE `log2` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `content` text NOT NULL COMMENT '内容', `status` tinyint(3) unsigned NOT NULL COMMENT '记录状态', `addtime` int(11) unsigned NOT NULL COMMENT '添加时间', `lastmodify` int(11) unsigned NOT NULL COMMENT '最后修改时间', PRIMARY KEY (`id`,`addtime`), KEY `id`(`id`), KEY `addtime`(`addtime`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (addtime) (PARTITION p201801 VALUES LESS THAN (unix_timestamp('2018-02-01 00:00:00')) ENGINE = InnoDB, PARTITION p201802 VALUES LESS THAN (unix_timestamp('2018-03-01 00:00:00')) ENGINE = InnoDB, PARTITION p201803 VALUES LESS THAN (unix_timestamp('2018-04-01 00:00:00')) ENGINE = InnoDB, PARTITION p201804 VALUES LESS THAN (unix_timestamp('2018-05-01 00:00:00')) ENGINE = InnoDB, PARTITION pmax VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */;
2.将log的数据复制到log2
insert into `log2` select * from `log`;
3.删除log表
drop table `log`;
4.将log2表改名为log
rename table `log2` to `log`;
执行后查看数据分区分布
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='test' AND TABLE_NAME = 'log';+----------------+------------+(北联网教程,专业提供视频软件下载)
……