Partition学习笔记
這幾天學(xué)習(xí)了一下分區(qū),不經(jīng)意間想起了三國(guó)演義開(kāi)篇詞:“合久必分,分久必合”,看來(lái)這句話也適合數(shù)據(jù)庫(kù),英文手冊(cè)和中文手冊(cè)對(duì)比,更新了很多東西,以后盡量看英文的了,下面是學(xué)習(xí)筆記:
理論部分:
1,基本概念:
分區(qū)可以設(shè)置任意大小的規(guī)則,跨文件系統(tǒng)分配單個(gè)表的多個(gè)部分。實(shí)際上,表的不同部分在不同的位置被存儲(chǔ)為單獨(dú)的表。用戶所選擇的、實(shí)現(xiàn)數(shù)據(jù)分割的規(guī)則被稱(chēng)為分區(qū)函數(shù),這在MySQL中它可以是模數(shù),或者是簡(jiǎn)單的匹配一個(gè)連續(xù)的數(shù)值區(qū)間或數(shù)值列表,或者是一個(gè)內(nèi)部HASH函數(shù),或一個(gè)線性HASH函數(shù)。函數(shù)根據(jù)用戶指定的分區(qū)類(lèi)型來(lái)選擇,把用戶提供的表達(dá)式的值作為參數(shù)。該表達(dá)式可以是一個(gè)整數(shù)列值,或一個(gè)作用在一個(gè)或多個(gè)列值上并返回一個(gè)整數(shù)的函數(shù)。這個(gè)表達(dá)式的值傳遞給分區(qū)函數(shù),分區(qū)函數(shù)返回一個(gè)表示那個(gè)特定記錄應(yīng)該保存在哪個(gè)分區(qū)的序號(hào)。這個(gè)函數(shù)不能是常數(shù),也不能是任意數(shù)。它不能包含任何查詢,但是實(shí)際上可以使用MySQL 中任何可用的SQL表達(dá)式,只要該表達(dá)式返回一個(gè)小于MAXVALUE(最大可能的正整數(shù))的正數(shù)值。
?
?
2,注意事項(xiàng):
對(duì)于創(chuàng)建了分區(qū)的表,可以使用你的MySQL 服務(wù)器所支持的任何存儲(chǔ)引擎;MySQL 分區(qū)引擎在一個(gè)單獨(dú)的層中運(yùn)行,并且可以和任何這樣的層進(jìn)行相互作用。在MySQL 5.1版中,同一個(gè)分區(qū)表的所有分區(qū)必須使用同一個(gè)存儲(chǔ)引擎;例如,不能對(duì)一個(gè)分區(qū)使用MyISAM,而對(duì)另一個(gè)使用InnoDB。但是,這并不妨礙在同一個(gè) MySQL 服務(wù)器中,甚至在同一個(gè)數(shù)據(jù)庫(kù)中,對(duì)于不同的分區(qū)表使用不同的存儲(chǔ)引擎。
?
分區(qū)適用于一個(gè)表的所有數(shù)據(jù)和索引;不能只對(duì)數(shù)據(jù)分區(qū)而不對(duì)索引分區(qū),反之亦然,同時(shí)也不能只對(duì)表的一部分進(jìn)行分區(qū)。
?
可以通過(guò)使用用來(lái)創(chuàng)建分區(qū)表的CREATE TABLE語(yǔ)句的PARTITION子句的DATA DIRECTORY(數(shù)據(jù)路徑)和INDEX DIRECTORY(索引路徑)選項(xiàng),為每個(gè)分區(qū)的數(shù)據(jù)和索引指定特定的路徑。此外,MAX_ROWS和MIN_ROWS選項(xiàng)可以用來(lái)設(shè)定最大和最小的行數(shù),它們可以各自保存在每個(gè)分區(qū)里。
?
無(wú)論使用何種類(lèi)型的分區(qū),分區(qū)總是在創(chuàng)建時(shí)就自動(dòng)的順序編號(hào),且從0開(kāi)始記錄,記住這一點(diǎn)非常重要。當(dāng)有一新行插入到一個(gè)分區(qū)表中時(shí),就是使用這些分區(qū)編號(hào)來(lái)識(shí)別正確的分區(qū)。例如,如果你的表使用4個(gè)分區(qū),那么這些分區(qū)就編號(hào)為0, 1, 2, 和3。對(duì)于RANGE和LIST分區(qū)類(lèi)型,確認(rèn)每個(gè)分區(qū)編號(hào)都定義了一個(gè)分區(qū),很有必要。對(duì)HASH分區(qū),使用的用戶函數(shù)必須返回一個(gè)大于0的整數(shù)值。對(duì)于KEY分區(qū),這個(gè)問(wèn)題通過(guò)MySQL服務(wù)器內(nèi)部使用的 哈希函數(shù)自動(dòng)進(jìn)行處理。
?
?
3,分區(qū)類(lèi)型:
| RANGE分區(qū) | 按照RANGE分區(qū)的表是通過(guò)如下一種方式進(jìn)行分區(qū)的,每個(gè)分區(qū)包含那些分區(qū)表達(dá)式的值位于一個(gè)給定的連續(xù)區(qū)間內(nèi)的行。這些區(qū)間要連續(xù)且不能相互重疊,使用VALUES LESS THAN ?操作符來(lái)進(jìn)行定義。 |
| lIST分區(qū) | 類(lèi)似于RANGE分區(qū),區(qū)別在于LIST分區(qū)是基于列值匹配一個(gè)離散值集合中的某個(gè)值來(lái)進(jìn)行選擇。 |
| HASH分區(qū) | HASH分區(qū)主要用來(lái)確保數(shù)據(jù)在預(yù)先確定數(shù)目的分區(qū)中平均分布。在RANGE和LIST分區(qū)中,必須明確指定一個(gè)給定的列值或列值集合應(yīng)該保存在哪個(gè)分區(qū)中;而在HASH分區(qū)中,MySQL 自動(dòng)完成這些工作,你所要做的只是基于將要被哈希的列值指定一個(gè)列值或表達(dá)式,以及指定被分區(qū)的表將要被分割成的分區(qū)數(shù)量。 |
| KEY分區(qū) | 類(lèi)似于HASH分區(qū),區(qū)別在于KEY分區(qū)只支持計(jì)算一列或多列,且MySQL 服務(wù)器提供其自身的哈希函數(shù)。必須有一列或多列包含整數(shù)值。 |
??
?
測(cè)試實(shí)例:
1,range測(cè)試實(shí)例:
說(shuō)明:employees表保存有20家音像店的職員記錄,這20家音像店的編號(hào)從1到20,按照這種分區(qū)方案,在商店1到5工作的職員相對(duì)應(yīng)的所有行被保存在分區(qū)P0中,商店6到10的職員保存在P1中,依次類(lèi)推,大于或等于16的職員都保存在P3中。PARTITION BY RANGE 語(yǔ)法要求每個(gè)分區(qū)都是按順序進(jìn)行定義,從最低到最高,AXVALUE 表示最大的可能的整數(shù)值,在將來(lái)的某個(gè)時(shí)候,當(dāng)商店數(shù)已經(jīng)增長(zhǎng)到25,30 或更多,可以使用ALTER TABLE語(yǔ)句為商店21-25, 26-30,等等增加新的分區(qū)。
?
mysql> CREATE TABLE employees (
??? ->???? id INT NOT NULL,
??? ->???? fname VARCHAR(30),
??? ->???? lname VARCHAR(30),
??? ->???? hired DATE NOT NULL DEFAULT '1970-01-01',
??? ->???? separated DATE NOT NULL DEFAULT '9999-12-31',
??? ->???? job_code INT NOT NULL,
??? ->???? store_id INT NOT NULL
??? -> )
??? -> PARTITION BY RANGE (store_id) (
??? -> PARTITION p0 VALUES LESS THAN (6),
??? -> PARTITION p1 VALUES LESS THAN (11),
??? -> PARTITION p2 VALUES LESS THAN (16),
??? -> PARTITION p3 VALUES LESS THAN MAXVALUE
??? -> );
Query OK, 0 rows affected (0.01 sec)
?
mysql> insert into employees values (1,'xiaoii','xiaowu01','1971-01-01','1972-01-01',1,5);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (2,'xiaokk','xiaowu01','1972-01-01','1973-01-01',2,6);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (3,'xiaoxx','xiaowu01','1974-01-01','1975-01-01',3,7);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (4,'xiaoyy','xiaowu01','1976-01-01','1977-01-01',4,11);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (5,'xiaocc','xiaowu01','1978-01-01','1979-01-01',5,12);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (6,'xiaoaa','xiaowu01','1980-01-01','1981-01-01',6,16);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (7,'xiaott','xiaowu01','1982-01-01','1983-01-01',7,17);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employees values (8,'xiaogg','xiaowu01','1984-01-01','1985-01-01',8,20);
Query OK, 1 row affected (0.00 sec)
?
mysql> select * from employees;
+----+--------+----------+------------+------------+----------+----------+
| id | fname? | lname??? | hired????? | separated? | job_code | store_id |
+----+--------+----------+------------+------------+----------+----------+
|? 1 | xiaoii | xiaowu01 | 1971-01-01 | 1972-01-01 |??????? 1 |??????? 5 |
|? 2 | xiaokk | xiaowu01 | 1972-01-01 | 1973-01-01 |??????? 2 |??????? 6 |
|? 3 | xiaoxx | xiaowu01 | 1974-01-01 | 1975-01-01 |??????? 3 |??????? 7 |
|? 4 | xiaoyy | xiaowu01 | 1976-01-01 | 1977-01-01 |??????? 4 |?????? 11 |
|? 5 | xiaocc | xiaowu01 | 1978-01-01 | 1979-01-01 |??????? 5 |?????? 12 |
|? 6 | xiaoaa | xiaowu01 | 1980-01-01 | 1981-01-01 |??????? 6 |?????? 16 |
|? 7 | xiaott | xiaowu01 | 1982-01-01 | 1983-01-01 |??????? 7 |?????? 17 |
|? 8 | xiaogg | xiaowu01 | 1984-01-01 | 1985-01-01 |??????? 8 |?????? 20 |
+----+--------+----------+------------+------------+----------+----------+
8 rows in set (0.00 sec)
?
mysql> explain partitions select * from employees;
+----+-------------+-----------+-------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table???? | partitions? | type | possible_keys | key? | key_len | ref? | rows | Extra |
+----+-------------+-----------+-------------+------+---------------+------+---------+------+------+-------+
|? 1 | SIMPLE????? | employees | p0,p1,p2,p3 | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 8 |?????? |
+----+-------------+-----------+-------------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
?
?
mysql> explain partitions select * from employees where store_id=5;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employees | p0?????? ??| ALL? | NULL????????? | NULL | NULL??? | NULL |??? 8 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employees where store_id=11;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employees | p2???????? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 8 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employees where store_id=16;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employees | p3???????? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 8 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
?
2,LIST測(cè)試實(shí)例:
假定有20個(gè)音像店,店面分布在4個(gè)地區(qū),如下表所示:
| 地區(qū) | 商店ID 號(hào) |
| 北區(qū) | 3, 5, 6, 9, 17 |
| 東區(qū) | 1, 2, 10, 11, 19, 20 |
| 西區(qū) | 4, 12, 13, 14, 18 |
| 中心區(qū) | 7, 8, 15, 16 |
?
?
mysql> CREATE TABLE employeeslist (
??? -> id INT NOT NULL,
??? -> fname VARCHAR(30),
??? -> lname VARCHAR(30),
??? -> hired DATE NOT NULL DEFAULT '1970-01-01',
??? -> separated DATE NOT NULL DEFAULT '9999-12-31',
??? -> job_code INT,
??? -> store_id INT
??? -> )
??? -> PARTITION BY LIST(store_id) (
??? -> PARTITION pNorth VALUES IN (3,5,6,9,17),
??? -> PARTITION pEast VALUES IN (1,2,10,11,19,20),
??? -> PARTITION pWest VALUES IN (4,12,13,14,18),
??? -> PARTITION pCentral VALUES IN (7,8,15,16)
??? -> );
Query OK, 0 rows affected (0.00 sec)
?
mysql> insert into employeeslist values (1,'xiaoii','xiaowu01','1971-01-01','1972-01-01',1,1);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employeeslist values (2,'xiaokk','xiaowu01','1972-01-01','1973-01-01',2,3);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employeeslist values (3,'xiaoxx','xiaowu01','1974-01-01','1975-01-01',3,4);
Query OK, 1 row affected (0.00 sec)
?
mysql> select * from employeeslist;
+----+--------+----------+------------+------------+----------+----------+
| id | fname? | lname??? | hired????? | separated? | job_code | store_id |
+----+--------+----------+------------+------------+----------+----------+
|? 2 | xiaokk | xiaowu01 | 1972-01-01 | 1973-01-01 |??????? 2 |??????? 3 |
|? 1 | xiaoii | xiaowu01 | 1971-01-01 | 1972-01-01 |??????? 1 |??????? 1 |
|? 3 | xiaoxx | xiaowu01 | 1974-01-01 | 1975-01-01 |??????? 3 |??????? 4 |
+----+--------+----------+------------+------------+----------+----------+
3 rows in set (0.00 sec)
?
mysql> explain partitions select * from employeeslist where store_id=1;
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???????? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employeeslist | pEast????? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 4 | Using where |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employeeslist where store_id=3;
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???????? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employeeslist | pNorth???? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 4 | Using where |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employeeslist where store_id=4;
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???????? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employeeslist | pWest????? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 4 | Using where |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
?
?
?
?
3,HASH測(cè)試實(shí)例:
?
mysql> CREATE TABLE employeeshash (
??? -> id INT NOT NULL,
??? -> fname VARCHAR(30),
??? -> lname VARCHAR(30),
??? -> hired DATE NOT NULL DEFAULT '1970-01-01',
??? -> separated DATE NOT NULL DEFAULT '9999-12-31',
??? -> job_code INT,
??? -> store_id INT
??? -> )
??? -> PARTITION BY HASH(store_id)
??? -> PARTITIONS 4;
Query OK, 0 rows affected (0.00 sec)
?
mysql> insert into employeeshash values (1,'xiaoii','xiaowu01','1971-01-01','1972-01-01',1,1);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employeeshash values (2,'xiaokk','xiaowu01','1972-01-01','1973-01-01',2,3);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employeeshash values (3,'xiaoxx','xiaowu01','1974-01-01','1975-01-01',3,6);
Query OK, 1 row affected (0.00 sec)
?
mysql> insert into employeeshash values (4,'xiaoxx','xiaowu01','1974-01-01','1975-01-01',3,9);
Query OK, 1 row affected (0.00 sec)
?
?
mysql> select * from employeeshash;
+----+--------+----------+------------+------------+----------+----------+
| id | fname? | lname??? | hired????? | separated? | job_code | store_id |
+----+--------+----------+------------+------------+----------+----------+
|? 1 | xiaoii | xiaowu01 | 1971-01-01 | 1972-01-01 |??????? 1 |??????? 1 |
|? 4 | xiaoxx | xiaowu01 | 1974-01-01 | 1975-01-01 |??????? 3 |??????? 9 |
|? 3 | xiaoxx | xiaowu01 | 1974-01-01 | 1975-01-01 |??????? 3 |??????? 6 |
|? 2 | xiaokk | xiaowu01 | 1972-01-01 | 1973-01-01 |??????? 2 |??????? 3 |
+----+--------+----------+------------+------------+----------+----------+
4 rows in set (0.00 sec)
?
mysql> explain partitions select * from employeeshash;
+----+-------------+---------------+-------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table???????? | partitions? | type | possible_keys | key? | key_len | ref? | rows | Extra |
+----+-------------+---------------+-------------+------+---------------+------+---------+------+------+-------+
|? 1 | SIMPLE????? | employeeshash | p0,p1,p2,p3 | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 5 |?????? |
+----+-------------+---------------+-------------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employeeshash where store_id=1;
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???????? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employeeshash | p1???????? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 5 | Using where |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employeeshash where store_id=6;
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???????? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employeeshash | p2???????? | ALL? | NULL????????? | NULL | NULL??? | NULL |??? 5 | Using where |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
?
mysql> explain partitions select * from employeeshash where store_id=3;
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table???????? | partitions | type | possible_keys | key? | key_len | ref? | rows | Extra?????? |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
|? 1 | SIMPLE????? | employeeshash | p3???????? | ALL? | NULL ?????????| NULL | NULL??? | NULL |??? 5 | Using where |
+----+-------------+---------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
??
?
4,子分區(qū)測(cè)試實(shí)例:
在MySQL 5.1中,可以對(duì)RANGE或LIST分區(qū)了的表再進(jìn)行子分區(qū)。子分區(qū)既可以使用HASH希分區(qū),也可以使用KEY分區(qū)。這也被稱(chēng)為復(fù)合分區(qū)(composite partitioning),子分區(qū)是分區(qū)表中每個(gè)分區(qū)的再次分割。例如,考慮下面的CREATE TABLE 語(yǔ)句:
?
mysql> CREATE TABLE ts (id INT, purchased DATE)
??? -> PARTITION BY RANGE(YEAR(purchased))
??? -> SUBPARTITION BY HASH(TO_DAYS(purchased))
??? -> SUBPARTITIONS 2
??? -> (
??? -> PARTITION p0 VALUES LESS THAN (1990),
??? -> PARTITION p1 VALUES LESS THAN (2000),
??? -> PARTITION p2 VALUES LESS THAN MAXVALUE
??? -> );
?
Query OK, 0 rows affected (0.03 sec)
mysql> desc ts;
+-----------+---------+------+-----+---------+-------+
| Field???? | Type??? | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id??????? | int(11) | YES? |???? | NULL??? |?????? |
| purchased | date??? | YES? |???? | NULL??? |?????? |
+-----------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
?
表ts 有3個(gè)RANGE分區(qū)。這3個(gè)分區(qū)中的每一個(gè)分區(qū)——p0, p1, 和 p2 ——又被進(jìn)一步分成了2個(gè)子分區(qū)。實(shí)際上,整個(gè)表被分成了3 * 2 = 6個(gè)分區(qū)。但是,由于PARTITION BY RANGE子句的作用,這些分區(qū)的頭2個(gè)只保存“purchased”列中值小于1990的那些記錄。
?
?
?
分區(qū)管理:
MySQL 5.1 可以通過(guò)使用ALTER TABLE 命令對(duì)分區(qū)進(jìn)行 添加、刪除、重新定義、合并或拆分。
?
1,RANGE和LIST分區(qū)的管理:
?
| ALTER TABLE chlotte DROP PARITION p1 ? | 刪除chlotte表中名稱(chēng)為p1的分區(qū)及p1分區(qū)中的所有數(shù)據(jù),但NDBCLUSTER存儲(chǔ)引擎不支持此語(yǔ)法。 |
| TRUNCATE TABLE chlotte; | 只刪除分區(qū)中的數(shù)據(jù),保留表的定義和表的分區(qū)模式。 |
| ALTER TABLE ... REORGANIZE PARTITION | 改變表的分區(qū)而又不丟失數(shù)據(jù)。 |
| ALTER TABLE ... ADD PARTITION | 增加一個(gè)新的RANGE或LIST分區(qū)到一個(gè)前面已經(jīng)分區(qū)了的表,對(duì)于RANGE分區(qū)的表,只可以使用ADD PARTITION添加新的分區(qū)到分區(qū)列表的高端。 |
| ALTER TABLE tbl_name REORGANIZE PARTITION partition_list INTO (partition_definitions); | 其中,tbl_name 是分區(qū)表的名稱(chēng),partition_list 是通過(guò)逗號(hào)分開(kāi)的、一個(gè)或多個(gè)將要被改變的現(xiàn)有分區(qū)的列,partition_definitions 是一個(gè)是通過(guò)逗號(hào)分開(kāi)的、新分區(qū)定義的列表,使用“REORGANIZE PARTITION”拆分或合并分區(qū),沒(méi)有數(shù)據(jù)丟失。 |
?
?
2,HASH和KEY分區(qū)的管理:
?
| ALTER TABLE chlotte COALESCE PARTITION 6; | 把chlotte表的分區(qū)數(shù)量由7個(gè)減少到6個(gè) |
| ALTER TABLE chlotte COALESCE PARTITION 8; | 把chlotte表的分區(qū)數(shù)量從6個(gè)增加到8個(gè) |
?
??
分區(qū)維護(hù):
?Table maintenance of partitioned tables can be accomplished using the statements CHECK TABLE, OPTIMIZE TABLE, ANALYZE TABLE, and REPAIR TABLE, which are supported for partitioned tables as of MySQL 5.1.27.
?
| ALTER TABLE t1 REBUILD PARTITION p0, p1; | 重建分區(qū): 這和先刪除保存在分區(qū)中的所有記錄,然后重新插入它們,具有同樣的效果。它可用于整理分區(qū)碎片。 |
| ALTER TABLE t1 OPTIMIZE PARTITION p0, p1; | 優(yōu)化分區(qū):如果從分區(qū)中刪除了大量的行,或者對(duì)一個(gè)帶有可變長(zhǎng)度的行作了許多修改,用來(lái)收回沒(méi)有使用的空間,并整理分區(qū)數(shù)據(jù)文件的碎片。 |
| ALTER TABLE t1 ANALYZE PARTITION p3; | 分析分區(qū):讀取并保存分區(qū)的鍵分布。 |
| ALTER TABLE t1 REPAIR PARTITION p0,p1; | 修復(fù)分區(qū): 修復(fù)被破壞的分區(qū)。 |
| ALTER TABLE trb3 CHECK PARTITION p1; | 檢查分區(qū): 可以使用幾乎與對(duì)非分區(qū)表使用CHECK TABLE 相同的方式檢查分區(qū)。 |
?
?
?
分區(qū)的限制:
1,從mysql 5.1.12 開(kāi)始不支持: 存儲(chǔ)過(guò)程,存儲(chǔ)函數(shù),用戶自定義函數(shù),用戶變量。
2,分區(qū)表不支持外鍵。
轉(zhuǎn)載于:https://blog.51cto.com/chlotte/372943
總結(jié)
以上是生活随笔為你收集整理的Partition学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: csv文件怎么打开?csv是什么文件?
- 下一篇: AAA学习笔记