先通过show plugins来查看服务器是否支持分区表
源码编译的要想支持分区表,需要设置 -DWITH_PARTITION_STORAGE_ENGINE 选项
分区的类型
mysql的分区类型要比oracle10g的分区类型多几种,下面是mysql的分区类型
CREATE TABLE members (
firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
username VARCHAR(16) NOT NULL,
email VARCHAR(35),
joined DATE NOT NULL
)
PARTITION BY RANGE COLUMNS(joined) (
PARTITION p0 VALUES LESS THAN (‘1960-01-01’),
PARTITION p1 VALUES LESS THAN (‘1970-01-01’),
PARTITION p2 VALUES LESS THAN (‘1980-01-01’),
PARTITION p3 VALUES LESS THAN (‘1990-01-01’),
PARTITION p4 VALUES LESS THAN MAXVALUE
);
对于范围分区,还有个扩展的范围列分区,就是不只是在1列上分区,而是在多列上范围分区,跟单列的范围分区在下面几点有所区别:
.test>insert into rc1 values(1,2);
Query OK, 1 row affected (0.02 sec)
.test>insert into rc1 values(5,2);
Query OK, 1 row affected (0.00 sec)
test>insert into rc1 values(5,10);
Query OK, 1 row affected (0.00 sec)
.test>insert into rc1 values(5,12);
Query OK, 1 row affected (0.00 sec)
test>insert into rc1 values(5,14);
Query OK, 1 row affected (0.00 sec)
test>insert into rc1 values(2,14);
Query OK, 1 row affected (0.01 sec)
test>select * from rc1;
+——+——+
| a | b |
+——+——+
| 1 | 2 |
| 5 | 2 |
| 5 | 10 |
| 2 | 14 |
| 5 | 12 |
| 5 | 14 |
+——+——+
6 rows in set (0.01 sec)
select partition_name,table_rows from information_schema.partitions where table_name=’rc1’;
+—————-+————+
| partition_name | table_rows |
+—————-+————+
| p0 | 4 |
| p3 | 2 |
+—————-+————+
2 rows in set (0.01 sec)
.test>select (1,2)<(5,12),(5,2)<(5,12),(5,10)<(5,12);
+————–+————–+—————+
| (1,2)<(5,12) | (5,2)<(5,12) | (5,10)<(5,12) |
+————–+————–+—————+
| 1 | 1 | 1 |
+————–+————–+—————+
1 row in set (0.01 sec)
test>select (5,12)<(5,12),(5,14)<(5,12),(2,14)<(5,12);
+—————+—————+—————+
| (5,12)<(5,12) | (5,14)<(5,12) | (2,14)<(5,12) |
+—————+—————+—————+
| 0 | 0 | 1 |
+—————+—————+—————+
1 row in set (0.01 sec)
test>select (6,1)<(5,12);
+————–+
| (6,1)<(5,12) |
+————–+
| 0 |
+————–+
列表分区,这个没有maxvalue
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,
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)
);
hash分区
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,
store_id INT
)
PARTITION BY HASH( YEAR(hired) )
PARTITIONS 4;
hash分区的方法就是取模
MOD(YEAR(‘2005-09-01’),4)
= MOD(2005,4)
= 1
5.6还支持线性hash,创建线性hash的语法就是多了个linear,具体hash算法参考官方文档
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,
store_id INT
)
PARTITION BY LINEAR HASH( YEAR(hired) )
PARTITIONS 4;
键分区,这个跟hash分区类似,键分区的列需要是主键的列,如果表上没有主键,有唯一索引,那么唯一键被用来做分区键。如果唯一键没有被定义成not null,那么会报错。
CREATE TABLE tm1 (
s1 CHAR(32) PRIMARY KEY
)
PARTITION BY KEY(s1)
PARTITIONS 10;
CREATE TABLE k1 (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20)
)
PARTITION BY KEY()
PARTITIONS 2;