MySQL建立表分區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) );
mysql執行分析參考:https://blog.csdn.net/weixin_45310179/article/details/99591496