检查是否支持分区
通过如下命令检查的Mysql是否支持partition
mysql> SHOW PLUGINS;
...
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
+----------------------------+----------+--------------------+---------+---------+
42 rows in set (0.12 sec)
或者通过查询 INFORMATION_SCHEMA.PLUGINS 表
SELECT
PLUGIN_NAME as Name,
PLUGIN_VERSION as Version,
PLUGIN_STATUS as Status
FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='STORAGE ENGINE';
+--------------------+---------+----------+
| Name | Version | Status |
+--------------------+---------+----------+
| binlog | 1.0 | ACTIVE |
| MEMORY | 1.0 | ACTIVE |
| MRG_MYISAM | 1.0 | ACTIVE |
| CSV | 1.0 | ACTIVE |
| MyISAM | 1.0 | ACTIVE |
| FEDERATED | 1.0 | DISABLED |
| InnoDB | 5.6 | ACTIVE |
| BLACKHOLE | 1.0 | ACTIVE |
| PERFORMANCE_SCHEMA | 0.1 | ACTIVE |
| ARCHIVE | 3.0 | ACTIVE |
| partition | 1.0 | ACTIVE |
+--------------------+---------+----------+
11 rows in set (0.01 sec)
由Oracle提供的MySQL 5.6社区版二进制是提供了分区功能支持的
如果你是源码安装,在cmake时要指定 -DWITH_PARTITION_STORAGE_ENGINE
如果你想disable分区支持,你可以在启动MySQL时附加–skip-partition选项
MERGE, CSV, or FEDERATED不支持创建分区表
注意
ENGINE子句要用在partitioning options前
CREATE TABLE ti (id INT, amount DECIMAL(7,2), tr_date DATE)
ENGINE=INNODB
PARTITION BY HASH( MONTH(tr_date) )
PARTITIONS 6;
无论哪种MySQL分区类型,要么分区表上没有主键/唯一键,要么分区表的主键/唯一键都必须包含分区键,也就是说不能使用主键/唯一键字段之外的其他字段分区。
下表不能分区,因为唯一键中没有包含分区键id列,即便该列为主键
CREATE TABLE tnp (
id INT NOT NULL AUTO_INCREMENT,
ref BIGINT NOT NULL,
name VARCHAR(255),
PRIMARY KEY pk (id),
UNIQUE KEY uk (name)
)
partition by range (id) (
partition p0 values less than(10),
partition p1 values less than(20),
partition p2 values less than(30)
);
ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function
CREATE TABLE tnp (
id INT NOT NULL AUTO_INCREMENT,
ref BIGINT NOT NULL,
name VARCHAR(255),
PRIMARY KEY pk (id),
UNIQUE KEY uk (id,name)
)
partition by range (id) (
partition p0 values less than(10),
partition p1 values less than(20),
partition p2 values less than(30)
);
MySQL支持只查询某个分区的数据
( DELETE, INSERT, REPLACE, UPDATE, and LOAD DATA, LOAD XML 也支持Partition selection)
CREATE TABLE tnp (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
PRIMARY KEY pk (id)
)
partition by range (id) (
partition p0 values less than(10),
partition p1 values less than(20),
partition p2 values less than(30)
);
mysql> insert into tnp values(1,'fan'),(11,'bo'),(21,'shi');
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from tnp;
+----+------+
| id | name |
+----+------+
| 1 | fan |
| 11 | bo |
| 21 | shi |
+----+------+
3 rows in set (0.00 sec)
mysql> select * from tnp partition(p0,p2);
+----+------+
| id | name |
+----+------+
| 1 | fan |
| 21 | shi |
+----+------+
2 rows in set (0.00 sec)