MySQL分割槽, 子分割槽以及對錄入Null值的處理情況

us_yunleiwang發表於2013-12-06

-- MySQL分割槽, 子分割槽以及對錄入Null值的處理情況. 看完官方文件做的筆記.

-- KEY Partitioning
Partitioning by key is similar to partitioning by hash, except that where hash partitioning employs a user-defined expression, the hashing function for key partitioning is supplied by the MySQL server. This internal hashing function is based on the same algorithm as PASSWORD(). 
KEY is used rather than HASH. 
KEY takes only a list of one or more column names. The column or columns used as the partitioning key must comprise part or all of the table's primary key, if the table has one.
KEY takes a list of zero or more column names. Where no column name is specified as the partitioning key, the table's primary key is used, if there is one. For example, the following CREATE TABLE statement is valid in MySQL 5.5: 

  1. mysql> CREATE TABLE k1 (  
  2.  ->     id INT NOT NULL PRIMARY KEY,  
  3.  ->     name VARCHAR(20)  
  4.  -> )  
  5.  -> PARTITION BY KEY()  
  6.  -> PARTITIONS 2;  
  7. Query OK, 0 rows affected (0.06 sec)  
  8.   
  9. If there is no primary key but there is a unique keythen the unique key is used for the partitioning key:  
  10. mysql> CREATE TABLE k2 (  
  11.  ->     id INT NOT NULL,  
  12.  ->     name VARCHAR(20),  
  13.  ->     UNIQUE KEY (id)  
  14.  -> )  
  15.  -> PARTITION BY KEY()  
  16.  -> PARTITIONS 2;  
  17. Query OK, 0 rows affected (0.02 sec)  



However, if the unique key column were not defined as NOT NULL, then the previous statement would fail.

In both of these cases, the partitioning key is the id column, even though it is not shown in the output of SHOW CREATE TABLE or in the PARTITION_EXPRESSION column of the INFORMATION_SCHEMA.PARTITIONS table.
As below:

  1. mysql>  SELECT t.TABLE_NAME, t.PARTITION_NAME,t.TABLE_ROWS  FROM INFORMATION_SCHEMA.PARTITIONS t WHERE table_name='k2';  
  2. +------------+----------------+------------+  
  3. | TABLE_NAME | PARTITION_NAME | TABLE_ROWS |  
  4. +------------+----------------+------------+  
  5. | k2         | p0             |          3 |  
  6. | k2         | p1             |          4 |  
  7. +------------+----------------+------------+  
  8. 2 rows in set (0.01 sec)  

Unlike the case with other partitioning types, columns used for partitioning by KEY are not restricted to integer or NULL values.
For example, the following CREATE TABLE statement is valid: 
沒有primary key,沒有在定義時候指定分割槽欄位,會抱錯:

  1. mysql> CREATE TABLE tm3 (  
  2.     ->     s1 CHAR(32)   
  3.     -> )  
  4.     -> PARTITION BY KEY()  
  5.     -> PARTITIONS 10;  
  6. ERROR 1488 (HY000): Field in list of fields for partition function not found in table  
  7. 在定義中加入分割槽欄位,add the column in define , it is ok  
  8. mysql> CREATE TABLE tm3 (  
  9.     ->     s1 CHAR(32)   
  10.     -> )  
  11.     -> PARTITION BY KEY(s1)  
  12.     -> PARTITIONS 10;  
  13. Query OK, 0 rows affected (0.07 sec)  
  14.   
  15. mysql>   

 

子分割槽 Subpartitioning
Subpartitioning—also known as composite partitioning—is the further division of each partition in a partitioned table.
For example, consider the following CREATE TABLE statement:

  1. mysql> CREATE TABLE ts (id INT, purchased DATE)  
  2.     ->     PARTITION BY RANGE( YEAR(purchased) )  
  3.     ->     SUBPARTITION BY HASH( TO_DAYS(purchased) ) (  
  4.     ->         PARTITION p0 VALUES LESS THAN (1990) (  
  5.     ->             SUBPARTITION s0,  
  6.     ->             SUBPARTITION s1  
  7.     ->         ),  
  8.     ->         PARTITION p1 VALUES LESS THAN (2000) (  
  9.     ->             SUBPARTITION s2,  
  10.     ->             SUBPARTITION s3  
  11.     ->         ),  
  12.     ->         PARTITION p2 VALUES LESS THAN MAXVALUE (  
  13.     ->             SUBPARTITION s4,  
  14.     ->             SUBPARTITION s5  
  15.     ->         )  
  16.     ->     );  
  17.   
  18. Query OK, 0 rows affected (0.04 sec)  
  19.   
  20.   
  21. CREATE TABLE ts3 (id INT, purchased DATE)  
  22.     PARTITION BY RANGE( YEAR(purchased) )  
  23.     SUBPARTITION BY HASH( TO_DAYS(purchased) ) (  
  24.         PARTITION p0 VALUES LESS THAN (1990) (  
  25.             SUBPARTITION s0,  
  26.             SUBPARTITION s1  
  27.         ),  
  28.         PARTITION p1 VALUES LESS THAN (2000),  
  29.         PARTITION p2 VALUES LESS THAN MAXVALUE (  
  30.             SUBPARTITION s2,  
  31.             SUBPARTITION s3  
  32.         )  
  33.     );  

 

(1) Each partition must have the same number of subpartitions. if not ,fail

  1. mysql> CREATE TABLE ts3 (id INT, purchased DATE)  
  2.  ->     PARTITION BY RANGE( YEAR(purchased) )  
  3.  ->     SUBPARTITION BY HASH( TO_DAYS(purchased) ) (  
  4.  ->         PARTITION p0 VALUES LESS THAN (1990) (  
  5.  ->             SUBPARTITION s0,  
  6.  ->             SUBPARTITION s1  
  7.  ->         ),  
  8.  ->         PARTITION p1 VALUES LESS THAN (2000),  
  9.  ->         PARTITION p2 VALUES LESS THAN MAXVALUE (  
  10.  ->             SUBPARTITION s2,  
  11.  ->             SUBPARTITION s3  
  12.  ->         )  
  13.  ->     );  
  14. ERROR 1064 (42000): Wrong number of subpartitions defined, mismatch with previous setting near '  
  15.   PARTITION p2 VALUES LESS THAN MAXVALUE (  
  16.    SUBPARTITION s2,  
  17.  ' at line 8  
  18. mysql>   



(2) Each SUBPARTITION clause must include (at a minimum) a name for the subpartition.

Otherwise, you may set any desired option for the subpartition or allow it to assume its default setting for that option.


(3) Subpartition names must be unique across the entire table.


(4)  Subpartitions can be used with especially large tables to distribute data and indexes across many disks. Suppose that you have 6 disks mounted as /disk0, /disk1, /disk2, and so on. Now consider the following example:

  1. mysql> CREATE TABLE ts5 (id INT, purchased DATE)  
  2.     ->     PARTITION BY RANGE( YEAR(purchased) )  
  3.     ->     SUBPARTITION BY HASH( TO_DAYS(purchased) ) (  
  4.     ->         PARTITION p0 VALUES LESS THAN (1990) (  
  5.     ->             SUBPARTITION s0  
  6.     ->                 DATA DIRECTORY = '/disk0/data'  
  7.     ->                 INDEX DIRECTORY = '/disk0/idx',  
  8.     ->             SUBPARTITION s1  
  9.     ->                 DATA DIRECTORY = '/disk1/data'  
  10.     ->                 INDEX DIRECTORY = '/disk1/idx'  
  11.     ->         ),  
  12.     ->         PARTITION p1 VALUES LESS THAN (2000) (  
  13.     ->             SUBPARTITION s2  
  14.     ->                 DATA DIRECTORY = '/disk2/data'  
  15.     ->                 INDEX DIRECTORY = '/disk2/idx',  
  16.     ->             SUBPARTITION s3  
  17.     ->                 DATA DIRECTORY = '/disk3/data'  
  18.     ->                 INDEX DIRECTORY = '/disk3/idx'  
  19.     ->         ),  
  20.     ->         PARTITION p2 VALUES LESS THAN MAXVALUE (  
  21.     ->             SUBPARTITION s4  
  22.     ->                 DATA DIRECTORY = '/disk4/data'  
  23.     ->                 INDEX DIRECTORY = '/disk4/idx',  
  24.     ->             SUBPARTITION s5  
  25.     ->                 DATA DIRECTORY = '/disk5/data'  
  26.     ->                 INDEX DIRECTORY = '/disk5/idx'  
  27.     ->         )  
  28.     ->     );  
  29. Query OK, 0 rows affected (0.04 sec)  
  30.   
  31. In this case, a separate disk is used for the data and for the indexes of each RANGE. Many other variations are possible;   
  1. another example might be:   
  2. mysql> CREATE TABLE ts6 (id INT, purchased DATE)  
  3.     ->     PARTITION BY RANGE(YEAR(purchased))  
  4.     ->     SUBPARTITION BY HASH( TO_DAYS(purchased) ) (  
  5.     ->         PARTITION p0 VALUES LESS THAN (1990) (  
  6.     ->             SUBPARTITION s0a  
  7.     ->                 DATA DIRECTORY = '/disk0'  
  8.     ->                 INDEX DIRECTORY = '/disk1',  
  9.     ->             SUBPARTITION s0b  
  10.     ->                 DATA DIRECTORY = '/disk2'  
  11.     ->                 INDEX DIRECTORY = '/disk3'  
  12.     ->         ),  
  13.     ->         PARTITION p1 VALUES LESS THAN (2000) (  
  14.     ->             SUBPARTITION s1a  
  15.     ->                 DATA DIRECTORY = '/disk4/data'  
  16.     ->                 INDEX DIRECTORY = '/disk4/idx',  
  17.     ->             SUBPARTITION s1b  
  18.     ->                 DATA DIRECTORY = '/disk5/data'  
  19.     ->                 INDEX DIRECTORY = '/disk5/idx'  
  20.     ->         ),  
  21.     ->         PARTITION p2 VALUES LESS THAN MAXVALUE (  
  22.     ->             SUBPARTITION s2a,  
  23.     ->             SUBPARTITION s2b  
  24.     ->         )  
  25.     ->     );  
  26. Query OK, 0 rows affected (0.04 sec)  


 

In future, when the number of purchases for the decade beginning with the year 2000 grows to a point where the default location no longer provides sufficient space, the corresponding rows can be moved using an ALTER TABLE ... REORGANIZE PARTITION statement. See Section 17.3, “Partition Management”, for an explanation of how this can be done.


The DATA DIRECTORY and INDEX DIRECTORY options are disallowed in partition definitions when the NO_DIR_IN_CREATE server SQL mode is in effect. Beginning with MySQL 5.5.5, these options are also disallowed when defining subpartitions (Bug#42954).

How MySQL Partitioning Handles NULL
Partitioning in MySQL does nothing to disallow NULL as the value of a partitioning expression,
whether it is a column value or the value of a user-supplied expression. Even though it is permitted to use NULL as the value of an expression that must otherwise yield an integer, it is important to keep in mind that NULL is not a number. MySQL's partitioning implementation treats NULL as being less than any non-NULL value, just as ORDER BY does.

 

This means that treatment of NULL varies between partitioning of different types, and may produce behavior which you do not expect if you are not prepared for it.
This being the case, we discuss in this section how each MySQL partitioning type handles NULL values when determining the partition in which a row should be stored,
and provide examples for each.

 

Handling of NULL with RANGE partitioning.  If you insert a row into a table partitioned by RANGE such that the column value used to determine the partition is NULL,
the row is inserted into the lowest partition. For example, consider these two tables in a database named p, created as follows:

 

(1) Rang Partition,OK
You can see the partitions created by these two CREATE TABLE statements using the following query against the PARTITIONS table in the INFORMATION_SCHEMA database:

  1. mysql> SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH  
  2.     ->    FROM INFORMATION_SCHEMA.PARTITIONS  
  3.     ->     WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME LIKE 't_';  
  4. +------------+----------------+------------+----------------+-------------+  
  5. | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH |  
  6. +------------+----------------+------------+----------------+-------------+  
  7. | t1         | p0             |          0 |              0 |       16384 |  
  8. | t1         | p1             |          0 |              0 |       16384 |  
  9. | t1         | p2             |          0 |              0 |       16384 |  
  10. | t2         | p0             |          0 |              0 |       16384 |  
  11. | t2         | p1             |          0 |              0 |       16384 |  
  12. | t2         | p2             |          0 |              0 |       16384 |  
  13. | t2         | p3             |          0 |              0 |       16384 |  
  14. | ts         | p0             |          0 |              0 |       16384 |  
  15. | ts         | p0             |          0 |              0 |       16384 |  
  16. | ts         | p1             |          0 |              0 |       16384 |  
  17. | ts         | p1             |          0 |              0 |       16384 |  
  18. | ts         | p2             |          0 |              0 |       16384 |  
  19. | ts         | p2             |          0 |              0 |       16384 |  
  20. +------------+----------------+------------+----------------+-------------+  
  21. 14 rows in set (0.00 sec)  

 

Now let us populate each of these tables with a single row containing a NULL in the column used as the partitioning key,
and verify that the rows were inserted using a pair of SELECT statements:

You can see which partitions are used to store the inserted rows by rerunning the previous query against INFORMATION_SCHEMA.PARTITIONS and inspecting the output:

  1. mysql> INSERT INTO t1 VALUES (NULL, 'mothra');  
  2. Query OK, 1 row affected (0.00 sec)  
  3.   
  4. mysql> INSERT INTO t2 VALUES (NULL, 'mothra');  
  5. Query OK, 1 row affected (0.00 sec)  
  6.   
  7. mysql> SELECT * FROM t1;  
  8. +------+--------+  
  9. | c1   | c2     |  
  10. +------+--------+  
  11. | NULL | mothra |  
  12. +------+--------+  
  13. 1 row in set (0.01 sec)  
  14.   
  15. mysql> SELECT * FROM t2;  
  16. +------+--------+  
  17. | c1   | c2     |  
  18. +------+--------+  
  19. | NULL | mothra |  
  20. +------+--------+  
  21. 1 row in set (0.00 sec)  
  22.   
  23. mysql> SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH    FROM INFORMATION_SCHEMA.PARTITIONS      
  1. WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME LIKE 't_';  
  2. +------------+----------------+------------+----------------+-------------+  
  3. | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH |  
  4. +------------+----------------+------------+----------------+-------------+  
  5. | t1         | p0             |          1 |          16384 |       16384 |  
  6. | t1         | p1             |          0 |              0 |       16384 |  
  7. | t1         | p2             |          0 |              0 |       16384 |  
  8. | t2         | p0             |          1 |          16384 |       16384 |  
  9. | t2         | p1             |          0 |              0 |       16384 |  
  10. | t2         | p2             |          0 |              0 |       16384 |  
  11. | t2         | p3             |          0 |              0 |       16384 |  
  12. | ts         | p0             |          0 |              0 |       16384 |  
  13. | ts         | p0             |          0 |              0 |       16384 |  
  14. | ts         | p1             |          0 |              0 |       16384 |  
  15. | ts         | p1             |          0 |              0 |       16384 |  
  16. | ts         | p2             |          0 |              0 |       16384 |  
  17. | ts         | p2             |          0 |              0 |       16384 |  
  18. +------------+----------------+------------+----------------+-------------+  
  19. 13 rows in set (0.00 sec)  
  20.   
  21.   
  22. You can also demonstrate that these rows were stored in the lowest partition of each table by dropping these partitions,   
  1. and then re-running the SELECT statements:   
  1.    

(2) Handling of NULL with LIST partitioning. 必須將null在定義中加入才能錄入null的分割槽資料

  1. mysql> CREATE TABLE ts3 (  
  2.     ->     c1 INT,  
  3.     ->     c2 VARCHAR(20)  
  4.     -> )  
  5.     -> PARTITION BY LIST(c1) (  
  6.     ->     PARTITION p0 VALUES IN (0, 3, 6),  
  7.     ->     PARTITION p1 VALUES IN (1, 4, 7, NULL),  
  8.     ->     PARTITION p2 VALUES IN (2, 5, 8)  
  9.     -> );  
  10. Query OK, 0 rows affected (0.01 sec)  

否則insert null的分割槽資料會抱錯: ERROR 1504 (HY000): Table has no partition for value NULL

 

(3) Handling of NULL with HASH and KEY partitioning. 

  1. mysql> CREATE TABLE th (  
  2.     ->     c1 INT,  
  3.     ->     c2 VARCHAR(20)  
  4.     -> )  
  5.     -> PARTITION BY HASH(c1)  
  6.     -> PARTITIONS 2;  
  7. Query OK, 0 rows affected (0.00 sec)  
  8.   
  9. There is no data record in beginnig.  
  10. mysql>   SELECT TABLE_NAME,PARTITION_NAME,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH  
  11.     ->          FROM INFORMATION_SCHEMA.PARTITIONS  
  12.     ->          WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME ='th';  
  13. +------------+----------------+------------+----------------+-------------+  
  14. | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH |  
  15. +------------+----------------+------------+----------------+-------------+  
  16. | th         | p0             |          0 |              0 |       16384 |  
  17. | th         | p1             |          0 |              0 |       16384 |  
  18. +------------+----------------+------------+----------------+-------------+  
  19. 2 rows in set (0.00 sec)  
  20.   
  21. mysql> INSERT INTO th VALUES (NULL, 'mothra'), (0, 'gigan');  
  22. Query OK, 2 rows affected (0.00 sec)  
  23. Records: 2  Duplicates: 0  Warnings: 0  
  24.   
  25. mysql> SELECT * FROM th;  
  26. +------+--------+  
  27. | c1   | c2     |  
  28. +------+--------+  
  29. | NULL | mothra |  
  30. |    0 | gigan  |  
  31. +------+--------+  
  32. 2 rows in set (0.00 sec)  
  33.   
  34. mysql>   SELECT TABLE_NAME,PARTITION_NAME,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH  
  35.     ->          FROM INFORMATION_SCHEMA.PARTITIONS  
  36.     ->          WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME ='th';  
  37. +------------+----------------+------------+----------------+-------------+  
  38. | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH |  
  39. +------------+----------------+------------+----------------+-------------+  
  40. | th         | p0             |          2 |           8192 |       16384 |  
  41. | th         | p1             |          0 |              0 |       16384 |  
  42. +------------+----------------+------------+----------------+-------------+  
  43. 2 rows in set (0.00 sec)  

Recall that for any integer N, the value of NULL MOD N is always NULL. For tables that are partitioned by HASH or KEY, this result is treated for determining the correct partition as 0. Checking the INFORMATION_SCHEMA.PARTITIONS table once again, we can see that both rows were inserted into partition p0:

 

MySQL對分割槽中null值得處理, rang,key,以及hash中,都是直接放入min的分割槽中. list分割槽中則是放入事先定義好的包含null的分割槽中,如果list分割槽事先沒有定義包含null值的分割槽,那麼錄入的時候會抱錯

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23490154/viewspace-1062504/,如需轉載,請註明出處,否則將追究法律責任。

相關文章