有分区的表与没有分区的表使用上没有太大的区别,但如果要对表进行重新分区,删除分区重建会删除数据,因此不可直接进行操作,需要使用一些特别的处理实现。
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 |+----------------+------------+
可以看到log表的数据已经按新分区存储。
本文介绍mysql重建表分区并保留数据的方法,更多相关内容请关注。
相关推荐:
php json_encode不支持对象私有属性的解决方法
php生成唯一requestid类的相关内容
js 基础 数据类型及转换 进制 操作符
以上就是介绍mysql重建表分区并保留数据的方法的详细内容。