mysql> show variables like '%partition%'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | have_partitioning | YES | +-------------------+-------+
分區類型
mysql> show create table emp; | emp | CREATE TABLE `emp` ( `id` int(11) NOT NULL, `store_id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 /*!50100 PARTITION BY RANGE (store_id) (PARTITION p0 VALUES LESS THAN (10) ENGINE = InnoDB) */ | 1 row in set (0.00 sec) mysql> insert into emp values(1,11); ERROR 1526 (HY000): Table has no partition for value 11 mysql> insert into emp values(1,10); ERROR 1526 (HY000): Table has no partition for value 10 mysql> insert into emp values(1,9); Query OK, 1 row affected (0.01 sec)只可以插入,鍵值范圍內的值;上面只能插入比10小的store_id;
mysql> alter table emp add partition (partition p1 values less than (20)); Query OK, 0 rows affected (0.06 sec) mysql> insert into emp values(2,18); Query OK, 1 row affected (0.00 sec)新增了一個分區p1,范圍小於20,大於等於p0 的10;分區增加應該是連續的從p0開始。 VALUES LESS THAN MAXVALUE :比最大值大的其他值的分區;list中不存在; 在5.5版本後,range支持非整數 分區;使用關鍵字 PARTITION BY RANGE COLUMNS (name),如下面例子:按時間分區;
PARTITION BY RANGE COLUMNS (TIME) ( PARTITION p0 VALUES LESS THAN ('1996-01-01'), PARTITION p2 VALUES LESS THAN ('2006-01-01') )在mysql5.1中分區日期處理上只有year(),to_days(),兩種;而5.5分區日期處理增加了to_seconds(),把日期轉化成秒; range分區功能特別適合以下兩種情況:
partition by list (id) ( partition p0 values in (3,5), partition p1 values in (1,10), partition p0 values in (4,6), partition p1 values in (2,8), )
插入只可以是,分區中枚舉出來的值;且沒有 像range 分區中 values less than maxvalue 這樣包含其他值在內的定義方式; 同樣,在mysql5.5 之後可以支持非整數分區:
partition by list columns (category) ( partition p0 values in ('lodging','food'), partition p1 values in ('good','bad'), partition p0 values in ('flights','groud') )
columns 分區支持: 注意:columns分區僅支持一個或多個字段名作為分區鍵,不支持表達式作為分區鍵,這個和不帶 columns的range,list 分區 有區別; 多字段分區是,columns 分區的一個亮點; 多字段分區鍵的比較就是多列排序,先根據a字段排序,再根據b字段排序,然後根據排序結果來分區存放數據。和range單字段分區排序的規則實際上是一致的。
partition by range colulmns (a,b)( partition p01 values less than (0,10), partition p02 values less than (10,10), partition p03 values less than (10,20) )假設 partition p01 values less than (ma,mb), 此時進行元組比較:(a<ma) or ( a=ma and b<mb); 即先比較a,如果a小於ma,則直接存,當a=ma且b<mb時 也是符合要求的; Hash分區 主要用來分散熱點讀,確保數據在預先確定個數的分區中盡可能平均分布; 在執行hash分區時,mysql會對分區鍵應用一個散列函數,以此確定數據應當放在N個分區中的哪個分區中。 hash分區:
alter table emp drop partition p1;增加:
alter table emp add partition (partition p3 values less than (2000) )
假設原有p1,p2,p3,現在要增加一個分區,px的命名隨意,但是values less than (xxx)一定是大於當前分區中最大值的;
拆分:將p3分區(2000--2015)分為兩個分區p2(2000-2005)和p3(2005-2015)alter table emp reorganize partition p3 into (partition p2 values less than (2005),partition p3 values less than (2015) )合並:將p3分區(30),p4(40) 合並成 p2(40);
mysql> alter table emp reorganize partition p3,p4 into (partition p2 values less than (40));
注意:p3,p4,合並後范圍只能變大,不能縮小;即 p2 范圍>=40;
- PARTITION BY RANGE (store_id) ( PARTITION p0 VALUES LESS THAN (10) ) - PARTITION BY RANGE COLUMNS (TIME) ( PARTITION p0 VALUES LESS THAN ('1996-01-01'),PARTITION p2 VALUES LESS THAN ('2006-01-01') ) - partition by range colulmns (a,b) ( partition p01 values less than (0,10) ) - VALUES LESS THAN MAXVALUE :比最大值大的其他值的分區;list中不存在; - partition by list (id) ( partition p0 values in (3,5)) - partition by list columns (category) ( partition p0 values in ('lodging','food') ) - partition by hash (id) partition 4; - alter table emp drop partition p1; - altertable emp add partition (partition p3 values less than (2000) ) - alter table emp reorganize partition p3 into (partition p2 values less than (2005),partition p3 values less than (2015) ) - alter table emp reorganize partition p3,p4 into (partition p2 values less than (40)); - alter table emp coalesce partition 2; - alter table emp add partition partition 8
總結