下文主要给大家带来mysql重建表如何实现分区并保留数据,希望这些文字能够带给大家实际用处,这也是我编辑mysql重建表如何实现分区并保留数据这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。
1.创建与原始表一样结构的新表,新分区。
2.将原始表中数据复制到新表。
3.删除原始表。
4.将新表名称改为原始表名称。
日志表原始结构如下,按id分区。
CREATE DATABASE `test`;use `test`;CREATE TABLE `log` ( `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`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (id) (PARTITION p10w VALUES LESS THAN (100000) ENGINE = InnoDB, PARTITION p20w VALUES LESS THAN (200000) ENGINE = InnoDB, PARTITION p50w VALUES LESS THAN (500000) ENGINE = InnoDB, PARTITION p100w VALUES LESS THAN (1000000) ENGINE = InnoDB, PARTITION pmax VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */;insert into `log`(content,status,addtime,lastmodify) values('content1',1, unix_timestamp('2018-01-11 00:00:00'), unix_timestamp('2018-01-11 00:00:00')), ('content2',1, unix_timestamp('2018-02-22 00:00:00'), unix_timestamp('2018-02-22 00:00:00')), ('content3',1, unix_timestamp('2018-03-31 00:00:00'), unix_timestamp('2018-03-31 00:00:00'));
查看数据分区分布
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='test' AND TABLE_NAME = 'log';+----------------+------------+| PARTITION_NAME | TABLE_ROWS | +----------------+------------+| p10w | 3 | | p20w | 0 | | p50w | 0 | | p100w | 0 || pmax | 0 | +----------------+------------+
日志数据需要按时间进行搜寻,因此需要按日志时间重建分区。
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';+----------------+------------+| PARTITION_NAME | TABLE_ROWS | +----------------+------------+| p201801 | 1 | | p201802 | 1 | | p201803 | 1 | | p201804 | 0 || pmax | 0 | +----------------+------------+
对于以上关于mysql重建表如何实现分区并保留数据,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。