分区表的管理range和list分区添加分区
删除分区
修改分区
合并或拆分分区
hash和key分区添加分区
删除分区
修改分区
合并或拆分分区
分区的维护重建分区
优化分区
分析分区
检查分区
修复分区
分区表的注意事项
总结
关于分区表的的分类和示例,请参考上篇文章。
分区表的管理
range和list分区
对于range和list两种类型的分区表的管理,接下来的各种演示示例:添加分区、删除分区、修改分区、合并拆分分区的各种操作,都是基于下面的这个range范围分区表来做演示,list类型的分区操作类似。
这个range范围分区表的建表语句和初始化数据如下所示:/*创建range范围分区表*/ drop table if exists range_partition_table; create table range_partition_table( id int auto_increment, code varchar(16), create_date date, primary key(id, create_date) ) partition by range columns(create_date) ( partition p1 values less than('2020-02-01'), /*p1分区不包含2020-02-01这一天的数据*/ partition p2 values less than('2020-03-01'), partition p3 values less than('2020-04-01'), partition p4 values less than('2020-05-01'), partition px values less than maxvalue ); /*插入测试数据*/ insert into range_partition_table(id, code, create_date) values (null, 'A', '2020-01-04'); insert into range_partition_table(id, code, create_date) values (null, 'B', '2020-01-31'); insert into range_partition_table(id, code, create_date) values (null, 'C', '2020-02-01'); insert into range_partition_table(id, code, create_date) values (null, 'D', '2020-02-29'); insert into range_partition_table(id, code, create_date) values (null, 'E', '2020-03-01'); insert into range_partition_table(id, code, create_date) values (null, 'F', '2020-03-31'); insert into range_partition_table(id, code, create_date) values (null, 'G', '2020-04-01'); insert into range_partition_table(id, code, create_date) values (null, 'H', '2020-04-30'); insert into range_partition_table(id, code, create_date) values (null, 'I', '2020-05-01'); insert into range_partition_table(id, code, create_date) values (null, 'J', '2020-05-31'); insert into range_partition_table(id, code, create_date) values (null, 'K', '2020-06-01'); insert into range_partition_table(id, code, create_date) values (null, 'L', '2020-06-30'); insert into range_partition_table(id, code, create_date) values (null, 'M', '2020-07-01');
原始range分区表的详细信息如下图所示:
添加分区
这里管理分区部分,我们都使用range范围分区来做演示,
如果要添加一个分区,则只需如下的SQL语句alter table range_partition_table add partition (partition p5 values less than ('2020-06-01'));
在添加分区的时候,需要注意如果你的分区表类型是range范围分区,并且制定了最后一个范围分区的范围边界为maxvalue,那么你不能直接在这样的一个range范围分区表上增加分区,否则会出现如下的错误:mysql> alter table range_partition_table add partition (partition p5 values less than ('2020-06-01')); ERROR 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition