FROM : http://blog.csdn.net/sunvince/article/details/7752662
mysql5.1的时候新增的partition,解决了比较简单的sharding的策略 mysql在已有表增加partition,mysql5.5才有,可以是innodb_file_per_table关闭状态. ##初始表 CREATE TABLE IF NOT EXISTS `ccc` ( `a` int(11) NOT NULL AUTO_INCREMENT, `b` int(11) NOT NULL, PRIMARY KEY (`a`) ) ENGINE=InnoDB; #更改表为partition ALTER TABLE ccc PARTITION BY RANGE(a)( PARTITION p0 VALUES LESS THAN (2), PARTITION p1 VALUES LESS THAN (10) ); #添加分区 ALTER TABLE ccc ADD PARTITION ( PARTITION p2 VALUES LESS THAN (20) ) #添加分区 ALTER TABLE ccc ADD PARTITION ( PARTITION p3 VALUES LESS THAN (30) ) #添加分区只能按value增加,所以不要使用 # PARTITION pz VALUES LESS THAN MAXVALUE
https://dev.mysql.com/doc/refman/5.1/zh/partitioning.html#partitioning-overview
ALTER TABLE tabName PARTITION BY RANGE (year(log_time)*100+month(log_time)) (PARTITION p201402 VALUES LESS THAN (201403), PARTITION p201403 VALUES LESS THAN (201404), PARTITION p201404 VALUES LESS THAN (201405), PARTITION p201405 VALUES LESS THAN (201406), PARTITION p201406 VALUES LESS THAN (201407), PARTITION p201407 VALUES LESS THAN (201408), PARTITION p201408 VALUES LESS THAN (201409), PARTITION p201409 VALUES LESS THAN (201410), PARTITION p201410 VALUES LESS THAN (201411), PARTITION p201411 VALUES LESS THAN (201412), PARTITION p201412 VALUES LESS THAN (201501), PARTITION p201501 VALUES LESS THAN (201502), PARTITION p201502 VALUES LESS THAN (201503), PARTITION p201503 VALUES LESS THAN (201504), PARTITION p201504 VALUES LESS THAN (201505), PARTITION p201505 VALUES LESS THAN (201506), PARTITION p201506 VALUES LESS THAN (201507), PARTITION p201507 VALUES LESS THAN (201508), PARTITION p201508 VALUES LESS THAN (201509), PARTITION p201509 VALUES LESS THAN (201510), PARTITION p201510 VALUES LESS THAN (201511), PARTITION p201511 VALUES LESS THAN (201512), PARTITION p201512 VALUES LESS THAN (201601), PARTITION p201601 VALUES LESS THAN (201602), PARTITION p201602 VALUES LESS THAN (201603), PARTITION p201603 VALUES LESS THAN (201604), PARTITION p201604 VALUES LESS THAN (201605), PARTITION p201605 VALUES LESS THAN (201606), PARTITION p201606 VALUES LESS THAN (201607), PARTITION p201607 VALUES LESS THAN (201608), PARTITION p201608 VALUES LESS THAN (201609), PARTITION p201609 VALUES LESS THAN (201610), PARTITION p201610 VALUES LESS THAN (201611), PARTITION p201611 VALUES LESS THAN (201612), PARTITION p201612 VALUES LESS THAN (201701), PARTITION p201701 VALUES LESS THAN (201702), PARTITION p201702 VALUES LESS THAN (201703), PARTITION p201703 VALUES LESS THAN (201704), PARTITION p201704 VALUES LESS THAN (201705), PARTITION p201705 VALUES LESS THAN (201706), PARTITION p201706 VALUES LESS THAN (201707), PARTITION p201707 VALUES LESS THAN (201708), PARTITION p201708 VALUES LESS THAN (201709), PARTITION p201709 VALUES LESS THAN (201710), PARTITION p201710 VALUES LESS THAN (201711), PARTITION p201711 VALUES LESS THAN (201712), PARTITION p201712 VALUES LESS THAN (201801), PARTITION p201801 VALUES LESS THAN (201802), PARTITION p201802 VALUES LESS THAN (201803), PARTITION p201803 VALUES LESS THAN (201804), PARTITION p201804 VALUES LESS THAN (201805), PARTITION p201805 VALUES LESS THAN (201806), PARTITION p201806 VALUES LESS THAN (201807), PARTITION p201807 VALUES LESS THAN (201808), PARTITION p201808 VALUES LESS THAN (201809), PARTITION p201809 VALUES LESS THAN (201810), PARTITION p201810 VALUES LESS THAN (201811), PARTITION p201811 VALUES LESS THAN (201812), PARTITION p201812 VALUES LESS THAN (201901), PARTITION p2019 VALUES LESS THAN MAXVALUE);
ALTER TABLE table PARTITION BY RANGE (year(clicktime)*100+month(clicktime)) (PARTITION p201309 VALUES LESS THAN (201310), PARTITION p201310 VALUES LESS THAN (201311), PARTITION p201311 VALUES LESS THAN (201312), PARTITION p201312 VALUES LESS THAN (201401), PARTITION p201401 VALUES LESS THAN (201402), PARTITION p201402 VALUES LESS THAN (201403), PARTITION p201403 VALUES LESS THAN (201404), PARTITION p201404 VALUES LESS THAN (201405), PARTITION p201405 VALUES LESS THAN (201406), PARTITION p201406 VALUES LESS THAN (201407), PARTITION p201407 VALUES LESS THAN (201408), PARTITION p201408 VALUES LESS THAN (201409), PARTITION p201409 VALUES LESS THAN (201410), PARTITION p201410 VALUES LESS THAN (201411), PARTITION p201411 VALUES LESS THAN (201412), PARTITION p201412 VALUES LESS THAN (201501), PARTITION p201501 VALUES LESS THAN (201502), PARTITION p201502 VALUES LESS THAN (201503), PARTITION p201503 VALUES LESS THAN (201504), PARTITION p201504 VALUES LESS THAN (201505), PARTITION p201505 VALUES LESS THAN (201506), PARTITION p201506 VALUES LESS THAN (201507), PARTITION p201507 VALUES LESS THAN (201508), PARTITION p201508 VALUES LESS THAN (201509), PARTITION p201509 VALUES LESS THAN (201510), PARTITION p201510 VALUES LESS THAN (201511), PARTITION p201511 VALUES LESS THAN (201512), PARTITION p201512 VALUES LESS THAN (201601), PARTITION p201601 VALUES LESS THAN (201602), PARTITION p201602 VALUES LESS THAN (201603), PARTITION p201603 VALUES LESS THAN (201604), PARTITION p201604 VALUES LESS THAN (201605), PARTITION p201605 VALUES LESS THAN (201606), PARTITION p201606 VALUES LESS THAN (201607), PARTITION p201607 VALUES LESS THAN (201608), PARTITION p201608 VALUES LESS THAN (201609), PARTITION p201609 VALUES LESS THAN (201610), PARTITION p201610 VALUES LESS THAN (201611), PARTITION p201611 VALUES LESS THAN (201612), PARTITION p201612 VALUES LESS THAN (201701), PARTITION p201701 VALUES LESS THAN (201702), PARTITION p201702 VALUES LESS THAN (201703), PARTITION p201703 VALUES LESS THAN (201704), PARTITION p201704 VALUES LESS THAN (201705), PARTITION p201705 VALUES LESS THAN (201706), PARTITION p201706 VALUES LESS THAN (201707), PARTITION p201707 VALUES LESS THAN (201708), PARTITION p201708 VALUES LESS THAN (201709), PARTITION p201709 VALUES LESS THAN (201710), PARTITION p201710 VALUES LESS THAN (201711), PARTITION p201711 VALUES LESS THAN (201712), PARTITION p201712 VALUES LESS THAN (201801), PARTITION p201801 VALUES LESS THAN (201802), PARTITION p201802 VALUES LESS THAN (201803), PARTITION p201803 VALUES LESS THAN (201804), PARTITION p201804 VALUES LESS THAN (201805), PARTITION p201805 VALUES LESS THAN (201806), PARTITION p201806 VALUES LESS THAN (201807), PARTITION p201807 VALUES LESS THAN (201808), PARTITION p201808 VALUES LESS THAN (201809), PARTITION p201809 VALUES LESS THAN (201810), PARTITION p201810 VALUES LESS THAN (201811), PARTITION p201811 VALUES LESS THAN (201812), PARTITION p201812 VALUES LESS THAN (201901), PARTITION p2019 VALUES LESS THAN MAXVALUE);
ALTER TABLE table PARTITION BY RANGE (to_days(act_time)) ( PARTITION p201309 VALUES LESS THAN (to_days('2013-10-01')), PARTITION p201310 VALUES LESS THAN (to_days('2013-11-01')), PARTITION p201311 VALUES LESS THAN (to_days('2013-12-01')), PARTITION p201312 VALUES LESS THAN (to_days('2014-01-01')), PARTITION p201401 VALUES LESS THAN (to_days('2014-02-01')), PARTITION p201402 VALUES LESS THAN (to_days('2014-03-01')), PARTITION p201403 VALUES LESS THAN (to_days('2014-04-01')), PARTITION p201404 VALUES LESS THAN (to_days('2014-05-01')), PARTITION p201405 VALUES LESS THAN (to_days('2014-06-01')), PARTITION p201406 VALUES LESS THAN (to_days('2014-07-01')), PARTITION p201407 VALUES LESS THAN (to_days('2014-08-01')), PARTITION p201408 VALUES LESS THAN (to_days('2014-09-01')), PARTITION p201409 VALUES LESS THAN (to_days('2014-10-01')), PARTITION p201410 VALUES LESS THAN (to_days('2014-11-01')), PARTITION p201411 VALUES LESS THAN (to_days('2014-12-01')), PARTITION p201412 VALUES LESS THAN (to_days('2015-01-01')), PARTITION p201501 VALUES LESS THAN (to_days('2015-02-01')), PARTITION p201502 VALUES LESS THAN (to_days('2015-03-01')), PARTITION p201503 VALUES LESS THAN (to_days('2015-04-01')), PARTITION p201504 VALUES LESS THAN (to_days('2015-05-01')), PARTITION p201505 VALUES LESS THAN (to_days('2015-06-01')), PARTITION p201506 VALUES LESS THAN (to_days('2015-07-01')), PARTITION p201507 VALUES LESS THAN (to_days('2015-08-01')), PARTITION p201508 VALUES LESS THAN (to_days('2015-09-01')), PARTITION p201509 VALUES LESS THAN (to_days('2015-10-01')), PARTITION p201510 VALUES LESS THAN (to_days('2015-11-01')), PARTITION p201511 VALUES LESS THAN (to_days('2015-12-01')), PARTITION p201512 VALUES LESS THAN (to_days('2016-01-01')), PARTITION p201601 VALUES LESS THAN (to_days('2016-02-01')), PARTITION p201602 VALUES LESS THAN (to_days('2016-03-01')), PARTITION p201603 VALUES LESS THAN (to_days('2016-04-01')), PARTITION p201604 VALUES LESS THAN (to_days('2016-05-01')), PARTITION p201605 VALUES LESS THAN (to_days('2016-06-01')), PARTITION p201606 VALUES LESS THAN (to_days('2016-07-01')), PARTITION p201607 VALUES LESS THAN (to_days('2016-08-01')), PARTITION p201608 VALUES LESS THAN (to_days('2016-09-01')), PARTITION p201609 VALUES LESS THAN (to_days('2016-10-01')), PARTITION p201610 VALUES LESS THAN (to_days('2016-11-01')), PARTITION p201611 VALUES LESS THAN (to_days('2016-12-01')), PARTITION p201612 VALUES LESS THAN (to_days('2017-01-01')), PARTITION p201701 VALUES LESS THAN (to_days('2017-02-01')), PARTITION p201702 VALUES LESS THAN (to_days('2017-03-01')), PARTITION p201703 VALUES LESS THAN (to_days('2017-04-01')), PARTITION p201704 VALUES LESS THAN (to_days('2017-05-01')), PARTITION p201705 VALUES LESS THAN (to_days('2017-06-01')), PARTITION p201706 VALUES LESS THAN (to_days('2017-07-01')), PARTITION p201707 VALUES LESS THAN (to_days('2017-08-01')), PARTITION p201708 VALUES LESS THAN (to_days('2017-09-01')), PARTITION p201709 VALUES LESS THAN (to_days('2017-10-01')), PARTITION p201710 VALUES LESS THAN (to_days('2017-11-01')), PARTITION p201711 VALUES LESS THAN (to_days('2017-12-01')), PARTITION p201712 VALUES LESS THAN (to_days('2018-01-01')), 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 p2019 VALUES LESS THAN MAXVALUE);
ALTER TABLE table PARTITION BY RANGE (unix_timestamp(act_time)) ( PARTITION p201309 VALUES LESS THAN (unix_timestamp('2013-10-01 00:00:00')), PARTITION p201310 VALUES LESS THAN (unix_timestamp('2013-11-01 00:00:00')), PARTITION p201311 VALUES LESS THAN (unix_timestamp('2013-12-01 00:00:00')), PARTITION p201312 VALUES LESS THAN (unix_timestamp('2014-01-01 00:00:00')), PARTITION p201401 VALUES LESS THAN (unix_timestamp('2014-02-01 00:00:00')), PARTITION p201402 VALUES LESS THAN (unix_timestamp('2014-03-01 00:00:00')), PARTITION p201403 VALUES LESS THAN (unix_timestamp('2014-04-01 00:00:00')), PARTITION p201404 VALUES LESS THAN (unix_timestamp('2014-05-01 00:00:00')), PARTITION p201405 VALUES LESS THAN (unix_timestamp('2014-06-01 00:00:00')), PARTITION p201406 VALUES LESS THAN (unix_timestamp('2014-07-01 00:00:00')), PARTITION p201407 VALUES LESS THAN (unix_timestamp('2014-08-01 00:00:00')), PARTITION p201408 VALUES LESS THAN (unix_timestamp('2014-09-01 00:00:00')), PARTITION p201409 VALUES LESS THAN (unix_timestamp('2014-10-01 00:00:00')), PARTITION p201410 VALUES LESS THAN (unix_timestamp('2014-11-01 00:00:00')), PARTITION p201411 VALUES LESS THAN (unix_timestamp('2014-12-01 00:00:00')), PARTITION p201412 VALUES LESS THAN (unix_timestamp('2015-01-01 00:00:00')), PARTITION p201501 VALUES LESS THAN (unix_timestamp('2015-02-01 00:00:00')), PARTITION p201502 VALUES LESS THAN (unix_timestamp('2015-03-01 00:00:00')), PARTITION p201503 VALUES LESS THAN (unix_timestamp('2015-04-01 00:00:00')), PARTITION p201504 VALUES LESS THAN (unix_timestamp('2015-05-01 00:00:00')), PARTITION p201505 VALUES LESS THAN (unix_timestamp('2015-06-01 00:00:00')), PARTITION p201506 VALUES LESS THAN (unix_timestamp('2015-07-01 00:00:00')), PARTITION p201507 VALUES LESS THAN (unix_timestamp('2015-08-01 00:00:00')), PARTITION p201508 VALUES LESS THAN (unix_timestamp('2015-09-01 00:00:00')), PARTITION p201509 VALUES LESS THAN (unix_timestamp('2015-10-01 00:00:00')), PARTITION p201510 VALUES LESS THAN (unix_timestamp('2015-11-01 00:00:00')), PARTITION p201511 VALUES LESS THAN (unix_timestamp('2015-12-01 00:00:00')), PARTITION p201512 VALUES LESS THAN (unix_timestamp('2016-01-01 00:00:00')), PARTITION p201601 VALUES LESS THAN (unix_timestamp('2016-02-01 00:00:00')), PARTITION p201602 VALUES LESS THAN (unix_timestamp('2016-03-01 00:00:00')), PARTITION p201603 VALUES LESS THAN (unix_timestamp('2016-04-01 00:00:00')), PARTITION p201604 VALUES LESS THAN (unix_timestamp('2016-05-01 00:00:00')), PARTITION p201605 VALUES LESS THAN (unix_timestamp('2016-06-01 00:00:00')), PARTITION p201606 VALUES LESS THAN (unix_timestamp('2016-07-01 00:00:00')), PARTITION p201607 VALUES LESS THAN (unix_timestamp('2016-08-01 00:00:00')), PARTITION p201608 VALUES LESS THAN (unix_timestamp('2016-09-01 00:00:00')), PARTITION p201609 VALUES LESS THAN (unix_timestamp('2016-10-01 00:00:00')), PARTITION p201610 VALUES LESS THAN (unix_timestamp('2016-11-01 00:00:00')), PARTITION p201611 VALUES LESS THAN (unix_timestamp('2016-12-01 00:00:00')), PARTITION p201612 VALUES LESS THAN (unix_timestamp('2017-01-01 00:00:00')), PARTITION p201701 VALUES LESS THAN (unix_timestamp('2017-02-01 00:00:00')), PARTITION p201702 VALUES LESS THAN (unix_timestamp('2017-03-01 00:00:00')), PARTITION p201703 VALUES LESS THAN (unix_timestamp('2017-04-01 00:00:00')), PARTITION p201704 VALUES LESS THAN (unix_timestamp('2017-05-01 00:00:00')), PARTITION p201705 VALUES LESS THAN (unix_timestamp('2017-06-01 00:00:00')), PARTITION p201706 VALUES LESS THAN (unix_timestamp('2017-07-01 00:00:00')), PARTITION p201707 VALUES LESS THAN (unix_timestamp('2017-08-01 00:00:00')), PARTITION p201708 VALUES LESS THAN (unix_timestamp('2017-09-01 00:00:00')), PARTITION p201709 VALUES LESS THAN (unix_timestamp('2017-10-01 00:00:00')), PARTITION p201710 VALUES LESS THAN (unix_timestamp('2017-11-01 00:00:00')), PARTITION p201711 VALUES LESS THAN (unix_timestamp('2017-12-01 00:00:00')), PARTITION p201712 VALUES LESS THAN (unix_timestamp('2018-01-01 00:00:00')), PARTITION p201801 VALUES LESS THAN (unix_timestamp('2018-02-01 00:00:00')), PARTITION p201802 VALUES LESS THAN (unix_timestamp('2018-03-01 00:00:00')), PARTITION p201803 VALUES LESS THAN (unix_timestamp('2018-04-01 00:00:00')), PARTITION p201804 VALUES LESS THAN (unix_timestamp('2018-05-01 00:00:00')), PARTITION p201805 VALUES LESS THAN (unix_timestamp('2018-06-01 00:00:00')), PARTITION p201806 VALUES LESS THAN (unix_timestamp('2018-07-01 00:00:00')), PARTITION p201807 VALUES LESS THAN (unix_timestamp('2018-08-01 00:00:00')), PARTITION p201808 VALUES LESS THAN (unix_timestamp('2018-09-01 00:00:00')), PARTITION p201809 VALUES LESS THAN (unix_timestamp('2018-10-01 00:00:00')), PARTITION p201810 VALUES LESS THAN (unix_timestamp('2018-11-01 00:00:00')), PARTITION p201811 VALUES LESS THAN (unix_timestamp('2018-12-01 00:00:00')), PARTITION p201812 VALUES LESS THAN (unix_timestamp('2019-01-01 00:00:00')), PARTITION p2019 VALUES LESS THAN MAXVALUE);
http://xuebinbin212.blog.163.com/blog/static/112167376201111294041677/