1.mysql支持的分區
range分區:行數據基於屬於一個給定連續區間的列值被放入分區,mysql5.5開始支持range columns的分區。
list分區:分區面向的是離散的值,mysql5.5支持list columns。
hash分區:根據用戶自定義的表達式的返回值來進行分區,返回值不能爲負數。
key分區:根據MYSQL數據庫提供的哈希函數來進行分區。
2.基本語法
CREATE TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options];
partition_options:
PARTITION BY
{ [LINEAR] HASH(expr)
| [LINEAR] KEY [ALGORITHM={1|2}] (column_list)
| RANGE{(expr) | COLUMNS(column_list)}
| LIST{(expr) | COLUMNS(column_list)} }
[PARTITIONS num]
[SUBPARTITION BY
{ [LINEAR] HASH(expr)
| [LINEAR] KEY [ALGORITHM={1|2}] (column_list) }
[SUBPARTITIONS num]
]
[(partition_definition [, partition_definition] ...)]
range和list支持子分區,但子分區只支持hash和key分區
3.分區限制
//提示:使用innodb建立分區表時,須要在配置文件添加innodb_file_per_table=1
(1)建立任何類型的分區,若是表中存在主鍵或惟一索引時,分區列必須是惟一索引的一個組成部分。
mysql> create table part1
(col1 int not null,
col2 varchar(50) not null,
col3 int not null,
col4 int not null,
unique key(col1,col2))
partition by hash(col3)能夠是列和自定義的表達式 整形數值
partitions 4;
錯誤: A PRIMARY KEY must include all columns in the table's partitioning
(2)惟一索引能夠爲NULL值,而且分區列只要是惟一索引的一個組成部分,不須要整個惟一索引都是分區列。
create table part3
(col1 int not null,
col2 varchar(50) not null,
col3 int not null,
col4 int not null,
unique key(col1,col2,col3,col4))
partition by hash(col3)
partitions 10;
(3)若是表中沒有指定主鍵,惟一索引,能夠指定任何一列爲分區列。
create table part3
(col1 int not null,
col2 varchar(50) not null,
col3 int not null,
col4 int not null)
partition by hash(col3)
partitions 10;
4.建立各類分區 5.5版本
###range分區
CREATE TABLE titles_range (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by range columns(from_date)
(partition p01 values less than ('1985-12-31'),
partition p02 values less than ('1986-12-31'),
partition p03 values less than ('1987-12-31'),
partition p04 values less than ('1988-12-31'),
partition p05 values less than ('1989-12-31'),
partition p06 values less than ('1990-12-31'),
partition p07 values less than ('1991-12-31'),
partition p08 values less than ('1992-12-31'),
partition p09 values less than ('1993-12-31'),
partition p10 values less than ('1994-12-31'),
partition p11 values less than ('1995-12-31'),
partition p12 values less than ('1996-12-31'),
partition p13 values less than ('1997-12-31'),
partition p14 values less than ('1998-12-31'),
partition p15 values less than ('1999-12-31'),
partition p16 values less than ('2000-12-31'),
partition p17 values less than ('2001-12-31'),
partition p18 values less than ('2002-12-31'),
partition p19 values less than ('3000-12-31')
);
explain partitions select * from titles_range where from_date < '1985-12-31';
###list分區
CREATE TABLE titles_list (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by list columns(title)
(
partition p0 values in ('Assistant Engineer'),
partition p1 values in ('Engineer'),
partition p2 values in ('Manager'),
partition p3 values in ('Senior Engineer'),
partition p4 values in ('Senior Staff'),
partition p5 values in ('Staff'),
partition p6 values in ('Technique Leader')
);
//查看執行計劃
explain partitions select * from titles_list where title = 'Manager';
###hash分區
CREATE TABLE titles (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by hash(emp_no)
partitions 4;
//查看執行計劃
explain partitions select * from titles where emp_no = 499999;
+----+-------------+-------------+------------+------+----------------+---------+---------+-------+------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------------+------+----------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | titles_hash | p3 | ref | PRIMARY,emp_no | PRIMARY | 4 | const | 1 | |
+----+-------------+-------------+------------+------+----------------+---------+---------+-------+------+-------+
###key分區
---passwrod()
CREATE TABLE titles_key (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by key(emp_no)
partitions 4;
//查看執行計劃
explain partitions select * from titles_key where emp_no = 499999;
+----+-------------+------------+------------+------+----------------+---------+---------+-------+------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------------+------+----------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | titles_key | p0 | ref | PRIMARY,emp_no | PRIMARY | 4 | const | 1 | |
+----+-------------+------------+------------+------+----------------+---------+---------+-------+------+-------+
mysql