MySQL创建表分区
create table erp_bill_index( id int primary key auto_increment, addtime datetime ); insert into erp_bill_index(addtime) values ('2018-02-01 12:00:00'), ('2018-03-01 12:00:00'), ('2018-04-01 12:00:00'), ('2018-05-01 12:00:00'), ('2018-06-01 12:00:00'), ('2018-07-01 12:00:00'), ('2018-08-01 12:00:00'), ('2018-09-01 12:00:00'), ('2018-10-01 12:00:00'), ('2018-11-01 12:00:00'), ('2018-12-01 12:00:00'), ('2019-01-01 12:00:00'), ('2019-02-01 12:00:00'), ('2019-03-01 12:00:00'), ('2019-04-01 12:00:00'), ('2019-05-01 12:00:00'), ('2019-06-01 12:00:00'), ('2019-07-01 12:00:00'), ('2019-08-01 12:00:00'), ('2019-09-01 12:00:00'), ('2019-10-01 12:00:00'), ('2019-11-01 12:00:00'), ('2019-12-01 12:00:00'); alter table erp_bill_index drop primary key;-- 删除主键,在删除主键的时候,这个自增会让该语句执行失败,先取消字段自增,然后执行该语句后,再加上自增 alter table erp_bill_index add primary key(id,addtime);-- 添加主键,(分区要求:分区中使用的字段必须都包含在主键当中) -- 创建分区(分区要求:分区中使用的字段必须都包含在主键当中) ALTER TABLE erp_bill_index PARTITION by RANGE(to_days(addtime)) ( PARTITION p201801 VALUES LESS THAN (to_days('2018-02-01')), PARTITION p201802 VALUES LESS THAN (to_days('2018-03-01')), PARTITION p201803 VALUES LESS THAN (to_days('2018-04-01')), PARTITION p201804 VALUES LESS THAN (to_days('2018-05-01')), PARTITION p201805 VALUES LESS THAN (to_days('2018-06-01')), PARTITION p201806 VALUES LESS THAN (to_days('2018-07-01')), PARTITION p201807 VALUES LESS THAN (to_days('2018-08-01')), PARTITION p201808 VALUES LESS THAN (to_days('2018-09-01')), PARTITION p201809 VALUES LESS THAN (to_days('2018-10-01')), PARTITION p201810 VALUES LESS THAN (to_days('2018-11-01')), PARTITION p201811 VALUES LESS THAN (to_days('2018-12-01')), PARTITION p201812 VALUES LESS THAN (to_days('2019-01-01')), PARTITION p201901 VALUES LESS THAN (to_days('2019-02-01')), PARTITION p201902 VALUES LESS THAN (to_days('2019-03-01')), PARTITION p201903 VALUES LESS THAN (to_days('2019-04-01')), PARTITION p300012 VALUES LESS THAN (9223372036854775807) -- 剩下的放在一个分区中,当需要对这部分进行再次分区的时候,需要先删除该分区,然后再添加多个分区 ); -- 删除分区 ALTER TABLE erp_bill_index DROP PARTITION p300012; -- 添加新的分区 alter table erp_bill_index add PARTITION ( PARTITION p201905 VALUES LESS THAN (to_days('2019-06-01')) ENGINE = InnoDB, PARTITION p300012 VALUES LESS THAN (9223372036854775807) );