您好,欢迎访问一九零五行业门户网

数据切分Mysql分区表的管理与维护_MySQL

mysql支持4种表的分区,即range与list、hash与key,其中range和list类似,按一种区间进行分区,hash与key类似,是按照某种算法对字段进行分区。
range与list分区管理:
案例:有一个聊天记录表,用户几千左右,已经对表按照用户进行一定粒度的水平分割,现仍然有部分表存储的记录比较多,于是按照下列方式有对表进行了分区,分区的好处是,可以动态改变分区,删除分区后,数据也一同被删除,如聊天记录只保存两年,那么你就可以按照时间进行分区,定期删除两年前的分区,动态创建新的的分区就能做到很好的数据维护。
分区表创建的语句如下:
drop table if exists `msgss`;create table `msgss` ( `id` bigint(20) unsigned not null auto_increment comment '表主键', `sender` int(10) unsigned not null comment '发送者id', `reciver` int(10) unsigned not null comment '接收者id', `msg_type` tinyint(3) unsigned not null comment '消息类型', `msg` varchar(225) not null comment '消息内容', `atime` int(10) unsigned not null comment '发送时间', `sub_id` tinyint(3) unsigned not null comment '部门id', primary key (`id`,`atime`,`sub_id`)) engine=innodb default charset=utf8/*********分区信息**************/partition by range (atime) subpartition by hash (sub_id) ( partition t0 values less than(1451577600) ( subpartition s0, subpartition s1, subpartition s2, subpartition s3, subpartition s4, subpartition s5 ), partition t1 values less than(1483200000) ( subpartition s6, subpartition s7, subpartition s8, subpartition s9, subpartition s10, subpartition s11 ), partition t2 values less than maxvalue ( subpartition s12, subpartition s13, subpartition s14, subpartition s15, subpartition s16, subpartition s17 ));
上述语句创建了三个按照range划分的主分区,每个主分区下面有六个按照hash划分的子分区。
插入测试数据:
insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash',unix_timestamp(now()),1);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 2',unix_timestamp(now()),2);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 3',unix_timestamp(now()),3);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 10',unix_timestamp(now()),10);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 7',unix_timestamp(now()),7);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 5',unix_timestamp(now()),5);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash',1451577607,1);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 2',1451577609,2);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 3',1451577623,3);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 10',1451577654,10);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 7',1451577687,7);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 5',1451577699,5);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash',1514736056,1);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 2',1514736066,2);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 3',1514736076,3);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 10',1514736086,10);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 7',1514736089,7);insert into `msgss`(`sender`,`reciver`,`msg_type`,`msg`,`atime`,`sub_id`) values(1,2,0,'hello hash 5',1514736098,5);
进行分区分析:
explain partitions select * from msgss;
可以检测到分区信息如下:
检测分区数据分布:
explain partitions select * from msgss where `atime`1451577600 and `atime`1483200000 and `atime`1514736000;
结果:第一条语句只扫描了t0的所有子分区,第二条语句只扫描了t1的所有子分区,第三四条分别只扫描了t2的所有子分区,证明表的分区和数据分布成功。
需求:目前已经是2017年,需要将2015年所有的聊天记录删除,但是保留2016年的聊天记录,并且2017年的数据也能正常按照分区进行存储。
实现以上需求,需要两步,第一步删除t0分区,第二步按照新规则重建分区。
删除分区语句:
alter table `msgss` drop partition t0;
重建分区语句:
alter table `msgss` partition by range (atime) subpartition by hash (sub_id) ( partition t0 values less than(1483200000) ( subpartition s0, subpartition s1, subpartition s2, subpartition s3, subpartition s4, subpartition s5 ), partition t1 values less than(1514736000) ( subpartition s6, subpartition s7, subpartition s8, subpartition s9, subpartition s10, subpartition s11 ), partition t2 values less than maxvalue ( subpartition s12, subpartition s13, subpartition s14, subpartition s15, subpartition s16, subpartition s17 ));
查询发现,15年的数据全部被删除,剩余的数据被重新分区并分布。
其它类似信息

推荐信息