ååºçä½ç¨ï¼æ°æ®åºæ§è½çæååç®åæ°æ®ç®¡ç
å¨æ«ææä½ä¸ï¼mysqlä¼åå¨åªæ«æä¿æ¤æ°æ®çé£ä¸ªååºä»¥åå°æ«æèå´è·å¾æ§è½çæé«ã
ååºææ¯ä½¿å¾æ°æ®ç®¡çåå¾ç®åï¼å é¤æ个ååºä¸ä¼å¯¹å¦å¤çååºé æå½±åï¼ååºæç³»ç»ç´æ¥ç®¡çä¸ç¨æ工干é¢ã
æ¥è¯¢å½åçmysqlæ°æ®åºçæ¬æ¯å¦æ¯æååº
show variables like '%partition%';
ååºç±»å
ãRANGE ååºãï¼
åºäºå±äºä¸ä¸ªç»å®è¿ç»åºé´çåå¼ï¼æå¤è¡åé ç»ååºã
Â
ãLIST ååºãï¼
类似äºæRANGEååºï¼åºå«å¨äºLISTååºæ¯åºäºåå¼å¹é ä¸ä¸ªç¦»æ£å¼éåä¸çæ个å¼æ¥è¿è¡éæ©ã
Â
ãHASHååºãï¼
åºäºç¨æ·å®ä¹ç表达å¼çè¿åå¼æ¥è¿è¡éæ©çååºï¼è¯¥è¡¨è¾¾å¼ä½¿ç¨å°è¦æå ¥å°è¡¨ä¸çè¿äºè¡çåå¼è¿è¡è®¡ç®ãè¿ä¸ªå½æ°å¯ä»¥å å«MySQLä¸ææçã产çéè´æ´æ°å¼çä»»ä½è¡¨è¾¾å¼ã
Â
ãKEYååºã
ååºï¼ç±»ä¼¼äºæHASHååºï¼åºå«å¨äºKEYååºåªæ¯æ计ç®ä¸åæå¤åï¼ä¸MySQLæå¡å¨æä¾å ¶èªèº«çåå¸å½æ°ãå¿ é¡»æä¸åæå¤åå å«æ´æ°å¼ã
Â
ãå¤åååºãï¼RANGEâHASH,   LISTâHASH,  RANGEâKey,    LISTâKey
[[email protected] ~]# mysql -uroot -p
Enter password:Â
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
[[email protected] ~]# mysql -uroot -p
Enter password:Â
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.28 Source distribution
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> show variables like '%partition%';
Empty set (0.00 sec)
ãRANGE ååºãï¼Â
 mysql> create table emp_age_range
   -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> age int
   -> )
   -> partition by range(age)
   -> (
   -> partition p1 values less than (10),
   -> partition p2 values less than (20),
   -> partition p3 values less than maxvalue
   -> );
Query OK, 0 rows affected (0.80 sec)
mysql> create table emp_birthdate_range
   -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> birthdate date not null,
   -> salary int
   -> )
   -> partition by range(year(birthdate))
   -> (
   -> partition p1 values less than (1980),
   -> partition p2 values less than (1990),
   -> partition p3 values less than maxvalue
   -> );
Query OK, 0 rows affected (0.50 sec)
ãLIST ååºãÂ
 mysql> create table emp_deptno_list
  -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> birthdate date not null,
   -> salary int
   -> )
   -> partition by list(deptno)
   -> (
   -> partition p1 values in (10),
   -> partition p2 values in (20),
   -> partition p3 values in (30)
   -> );
Query OK, 0 rows affected (0.66 sec)
ãHASHååºã
 mysql> create table emp_birthday_hash
   -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> birthdate date not null,
   -> salary int
   -> )
   -> partition by hash(year(birthdate))
   -> partitions 4;
Query OK, 0 rows affected (0.41 sec)
ãKEYååºã
 mysql> create table emp_birthdate_key
   -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> birthdate date not null,
   -> salary int
   -> )
   -> partition by key(birthdate)
   -> partitions 4;
Query OK, 0 rows affected (1.00 sec)
ãå¤åååºã
 mysql> create table emp_birthdate_range_hash
   -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> birthdate date not null,
   -> salary int
   -> )
   -> partition by range(salary)
   -> subpartition by hash(year(birthdate))
   -> subpartitions 3
   -> (
   -> partition p1 values less than (2000),
   -> partition p2 values less than maxvalue
   -> );
Query OK, 0 rows affected (0.56 sec)
mysql> create table emp_salary_range_key
   -> (empno varchar(20) not null ,
   -> empname varchar(20),
   -> deptno int,
   -> birthdate date not null,
   -> salary int
   -> )
   -> partition by range(salary)
   -> subpartition by key(birthdate)
   -> subpartitions 3
   -> (
   -> partition p1 values less than (2000),
   -> partition p2 values less than maxvalue
   -> );
Query OK, 0 rows affected (0.62 sec)
mysql> CREATE TABLE emp_birthdate_list_hash (
   -> empno varchar(20) NOT NULL,
   -> empname varchar(20) ,
   -> deptno int,
   -> birthdate date NOT NULL,
   -> salary int
   ->Â
   -> )
   -> PARTITION BY list (deptno)
   -> subpartition by hash(year(birthdate))
   -> subpartitions 3
   -> (
   -> PARTITION p1 VALUES in (10),
   -> PARTITION p2 VALUES in (20)
   -> );
Query OK, 0 rows affected (0.55 sec)
mysql> CREATE TABLE emp_list_key (
   -> empno varchar(20) NOT NULL,
   -> empname varchar(20) ,
   -> deptno int,
   -> birthdate date NOT NULL,
   -> salary int
   -> )
   -> PARTITION BY list (deptno)
   -> subpartition by key(birthdate)
   -> subpartitions 3
   -> (
   -> PARTITION p1 VALUES in (10),
   -> PARTITION p2 VALUES in (20)
   -> );
Query OK, 0 rows affected (0.88 sec)
mysql> show tables;
+--------------------------+
| Tables_in_test          |
+--------------------------+
| emp_age_range           |
| emp_birthdate_key       |
| emp_birthdate_list_hash |
| emp_birthdate_range     |
| emp_birthdate_range_hash |
| emp_birthday_hash       |
| emp_deptno_list         |
| emp_list_key            |
| emp_salary_range_key    |
+--------------------------+
9 rows in set (0.00 sec)
1.å¯ä»¥æ¥çå建ååºè¡¨çcreateè¯å¥Â  show create table 表å
2.å¯ä»¥æ¥ç表æ¯ä¸æ¯ååºè¡¨Â     show table statusÂ
3.æ¥çinformation_schema.partitions表 ï¼å¯ä»¥æ¥çè¡¨å ·æåªå 个ååºãååºçæ¹æ³ãååºä¸æ°æ®çè®°å½æ°çä¿¡æ¯
selectÂ
 *
from information_schema.partitions whereÂ
 table_schema = schema() Â
 and table_name='test'; Â
4.æ¥çSQLæ§è¡è®¡åï¼explain partitions selectè¯å¥
éè¿æ¤è¯å¥æ¥æ¾ç¤ºæ«æåªäºååºï¼åä»ä»¬æ¯å¦ä½ä½¿ç¨ç.
mysql> desc emp_age_range;
+---------+-------------+------+-----+---------+-------+
| Field  | Type       | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| empno  | varchar(20) | NO  |    | NULL   |      |
| empname | varchar(20) | YESÂ |Â Â Â Â | NULLÂ Â Â |Â Â Â Â Â Â |
| deptno | int(11)    | YES |    | NULL   |      |
| age    | int(11)    | YES |    | NULL   |      |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql>Â show create table emp_age_range;
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table        | Create Table                                                                                                                                                                                                                                                                                                                                                                                                     |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp_age_range | CREATE TABLE `emp_age_range` (
 `empno` varchar(20) NOT NULL,
 `empname` varchar(20) DEFAULT NULL,
 `deptno` int(11) DEFAULT NULL,
 `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
|
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)
mysql> selectÂ
   ->  partition_name part, Â
   ->  partition_expression expr, Â
   ->  partition_description descr, Â
   ->  table_rows Â
   -> from information_schema.partitions whereÂ
   ->  table_schema = schema() ;
+------+-----------------+----------+------------+
| part | expr           | descr   | table_rows |
+------+-----------------+----------+------------+
| p1  | age            | 10      |         0 |
| p2  | age            | 20      |         0 |
| p3  | age            | MAXVALUE |         0 |
| p0Â Â | `birthdate`Â Â Â Â | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p1Â Â | `birthdate`Â Â Â Â | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p2Â Â | `birthdate`Â Â Â Â | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p3Â Â | `birthdate`Â Â Â Â | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p1  | deptno         | 10      |         0 |
| p1  | deptno         | 10      |         0 |
| p1  | deptno         | 10      |         0 |
| p2  | deptno         | 20      |         0 |
| p2  | deptno         | 20      |         0 |
| p2  | deptno         | 20      |         0 |
| p1Â Â | year(birthdate) | 1980Â Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p2Â Â | year(birthdate) | 1990Â Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p3Â Â | year(birthdate) | MAXVALUE |Â Â Â Â Â Â Â Â Â 0 |
| p1  | salary         | 2000    |         0 |
| p1  | salary         | 2000    |         0 |
| p1  | salary         | 2000    |         0 |
| p2  | salary         | MAXVALUE |         0 |
| p2  | salary         | MAXVALUE |         0 |
| p2  | salary         | MAXVALUE |         0 |
| p0Â Â | year(birthdate) | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p1Â Â | year(birthdate) | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p2Â Â | year(birthdate) | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p3Â Â | year(birthdate) | NULLÂ Â Â Â |Â Â Â Â Â Â Â Â Â 0 |
| p1  | deptno         | 10      |         0 |
| p2  | deptno         | 20      |         0 |
| p3  | deptno         | 30      |         0 |
| p1  | deptno         | 10      |         0 |
| p1  | deptno         | 10      |         0 |
| p1  | deptno         | 10      |         0 |
| p2  | deptno         | 20      |         0 |
| p2  | deptno         | 20      |         0 |
| p2  | deptno         | 20      |         0 |
| p1  | salary         | 2000    |         0 |
| p1  | salary         | 2000    |         0 |
| p1  | salary         | 2000    |         0 |
| p2  | salary         | MAXVALUE |         0 |
| p2  | salary         | MAXVALUE |         0 |
| p2  | salary         | MAXVALUE |         0 |
+------+-----------------+----------+------------+
41 rows in set (0.01 sec)
mysql> selectÂ
   ->  *
   -> from information_schema.partitions whereÂ
   ->  table_schema = schema() ;
è·åãä¸è½½å°åã ä¼ä¸æ¡æ¶æºç