mysql表分區(qū)主要分為RANGE分區(qū),LIST分區(qū)糟把,HASH分區(qū),LINEAR HASH分區(qū)牲剃,KEY分區(qū)
目前糊饱,我所使用的是RANGE分區(qū),其中主要注意兩點(diǎn):
1.分區(qū)所用的字段必須是主鍵颠黎,并且另锋,如果有設(shè)置唯一鍵,該字段也需要添加到唯一鍵中狭归,否則會(huì)報(bào)錯(cuò)夭坪。
創(chuàng)建表時(shí),建立分區(qū)的sql如下所示:
CREATETABLE`T_part` (
? ? ? `f_id`INTDEFAULTNULL,
? ? ? `f_name`VARCHAR(20)DEFAULTNULL,
? ? ? ?PRIMARYKEY(`f_id`)
) ENGINE=myisamDEFAULTCHARSET=utf8
PARTITIONBYRANGE (f_id)(-----指定分區(qū)方式
? ? ? PARTITION p0VALUESless THAN (10),--分了兩個(gè)區(qū)
? ? ? PARTITION p1VALUESless THAN (20)
);
修改表过椎,建立分區(qū)時(shí)sql如下所示:
ALTER TABLE wms_pac_package_detail PARTITION BY RANGE(TO_DAYS(`created_at`))
(
? ? ?PARTITION p0 VALUES LESS THAN (TO_DAYS('2017-04-01')) ENGINE = INNODB,
? ? ?PARTITION p1 VALUES LESS THAN (TO_DAYS('2017-05-01')) ENGINE = INNODB,
? ? ?PARTITION p2 VALUES LESS THAN (TO_DAYS('2017-06-01')) ENGINE = INNODB,
? ? ?PARTITION p3 VALUES LESS THAN (TO_DAYS('2017-07-01')) ENGINE = INNODB,
? ? ?PARTITION p4 VALUES LESS THAN (TO_DAYS('2017-08-01')) ENGINE = INNODB,
? ? ?PARTITION p5 VALUES LESS THAN (TO_DAYS('2017-09-01')) ENGINE = INNODB,
? ? ?PARTITION p6 VALUES LESS THAN (TO_DAYS('2017-10-01')) ENGINE = INNODB,
? ? ?PARTITION p7 VALUES LESS THAN (TO_DAYS('2017-11-01')) ENGINE = INNODB,
? ? ?PARTITION p8 VALUES LESS THAN (TO_DAYS('2017-12-01')) ENGINE = INNODB,
? ? ?PARTITION p9 VALUES LESS THAN (TO_DAYS('2018-01-01')) ENGINE = INNODB,
? ? ?PARTITION p10 VALUES LESS THAN MAXVALUE ENGINE = INNODB
);