mysql 分区
重建主键,并分区
ALTER TABLE `tb_pub_app_income` MODIFY COLUMN `end_date` datetime NOT NULL COMMENT '结束日期' AFTER `start_date`, DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `end_date`); alter table tb_pub_app_income PARTITION BY RANGE COLUMNS(end_date)( PARTITION p0 VALUES LESS THAN ('2017-4-1') , PARTITION p201704 VALUES LESS THAN ('2017-5-1') , PARTITION p201705 VALUES LESS THAN ('2017-6-1') , PARTITION p201706 VALUES LESS THAN ('2017-7-1') , PARTITION p201707 VALUES LESS THAN ('2017-8-1') , PARTITION p201708 VALUES LESS THAN ('2017-9-1') , PARTITION p201709 VALUES LESS THAN ('2017-10-1') , PARTITION p201710 VALUES LESS THAN ('2017-11-1') , PARTITION p201711 VALUES LESS THAN ('2017-12-1') , PARTITION p201712 VALUES LESS THAN ('2018-1-1') );
追加分区
alter table tb_pub_app_income add PARTITION( PARTITION p201801 VALUES LESS THAN ('2018-2-1') );
追加分区时报 VALUES LESS THAN value must be strictly increasing for each partition 错的原因是 最后分区太大了 ,将对应分区删除即可
/*!50500 PARTITION BY RANGE COLUMNS(end_date) (PARTITION p0 VALUES LESS THAN ('2016-10-1') ENGINE = InnoDB, PARTITION p201610 VALUES LESS THAN ('2016-11-1') ENGINE = InnoDB, PARTITION p201611 VALUES LESS THAN ('2016-12-1') ENGINE = InnoDB, PARTITION p201612 VALUES LESS THAN ('2017-1-1') ENGINE = InnoDB, PARTITION p201701 VALUES LESS THAN ('2017-2-1') ENGINE = InnoDB, PARTITION p201702 VALUES LESS THAN ('2017-3-1') ENGINE = InnoDB, PARTITION p201703 VALUES LESS THAN ('2017-4-1') ENGINE = InnoDB, PARTITION p201704 VALUES LESS THAN ('2017-5-1') ENGINE = InnoDB, PARTITION p201705 VALUES LESS THAN ('2017-6-1') ENGINE = InnoDB, PARTITION p201706 VALUES LESS THAN ('2017-7-1') ENGINE = InnoDB, PARTITION p201707 VALUES LESS THAN ('2017-8-1') ENGINE = InnoDB, PARTITION p201708 VALUES LESS THAN ('2017-9-1') ENGINE = InnoDB, PARTITION p201709 VALUES LESS THAN ('2017-10-1') ENGINE = InnoDB, PARTITION p201710 VALUES LESS THAN ('2017-11-1') ENGINE = InnoDB, PARTITION p201711 VALUES LESS THAN ('2017-12-1') ENGINE = InnoDB, PARTITION p201712 VALUES LESS THAN ('2018-1-1') ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN (MAXVALUE) ENGINE = InnoDB) */;
删除分区
alter table tb_pub_app_income drop PARTITION p1;
参考资料:
1、http://blog.csdn.net/tjcyjd/article/details/11194489
2、 http://bbs.csdn.net/topics/390121058