mysql partition 分区功能使用详解
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-29 17:20:26 浏览: 评论:0
= 最终结论 =
* 对于大数据量,建议使用分区功能.
* 去除不必要的字段.
* 根据手册,增加myisam_max_sort_file_size 会增加分区性能.
[分区命令详解],代码如下:
- = 分区例子 =
- * RANGE 类型
- CREATE TABLE users (
- uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL DEFAULT '',
- email VARCHAR(30) NOT NULL DEFAULT ''
- )
- PARTITION BY RANGE (uid) (
- PARTITION p0 VALUES LESS THAN (3000000)
- DATA DIRECTORY = '/data0/data'
- INDEX DIRECTORY = '/data1/idx',
- PARTITION p1 VALUES LESS THAN (6000000)
- DATA DIRECTORY = '/data2/data'
- INDEX DIRECTORY = '/data3/idx',
- PARTITION p2 VALUES LESS THAN (9000000)
- DATA DIRECTORY = '/data4/data'
- INDEX DIRECTORY = '/data5/idx',
- PARTITION p3 VALUES LESS THAN MAXVALUE DATA DIRECTORY = '/data6/data'
- INDEX DIRECTORY = '/data7/idx'
- );
在这里,将用户表分成4个分区,以每300万条记录为界限,每个分区都有自己独立的数据、索引文件的存放目录,与此同时,这些目录所在的物理磁盘分区可能也都是完全独立的,可以提高磁盘IO吞吐量.
LIST 类型,代码如下:
- CREATE TABLE category (
- cid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL DEFAULT ''
- )
- PARTITION BY LIST (cid) (
- PARTITION p0 VALUES IN (0,4,8,12)
- DATA DIRECTORY = '/data0/data'
- INDEX DIRECTORY = '/data1/idx',
- PARTITION p1 VALUES IN (1,5,9,13)
- DATA DIRECTORY = '/data2/data'
- INDEX DIRECTORY = '/data3/idx',
- PARTITION p2 VALUES IN (2,6,10,14)
- DATA DIRECTORY = '/data4/data'
- INDEX DIRECTORY = '/data5/idx',
- PARTITION p3 VALUES IN (3,7,11,15)
- DATA DIRECTORY = '/data6/data'
- INDEX DIRECTORY = '/data7/idx'
- );
分成4个区,数据文件和索引文件单独存放.
HASH 类型,代码如下:
- CREATE TABLE users (
- uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL DEFAULT '',
- email VARCHAR(30) NOT NULL DEFAULT ''
- )
- PARTITION BY HASH (uid) PARTITIONS 4 (
- PARTITION p0
- DATA DIRECTORY = '/data0/data'
- INDEX DIRECTORY = '/data1/idx',
- --phpfensi.com
- PARTITION p1
- DATA DIRECTORY = '/data2/data'
- INDEX DIRECTORY = '/data3/idx',
- PARTITION p2
- DATA DIRECTORY = '/data4/data'
- INDEX DIRECTORY = '/data5/idx',
- PARTITION p3
- DATA DIRECTORY = '/data6/data'
- INDEX DIRECTORY = '/data7/idx'
- );
分成4个区,数据文件和索引文件单独存放,例子,代码如下:
- CREATE TABLE ti2 (id INT, amount DECIMAL(7,2), tr_date DATE)
- ENGINE=myisam
- PARTITION BY HASH( MONTH(tr_date) )
- PARTITIONS 6;
- CREATE PROCEDURE load_ti2()
- begin
- declare v int default 0;
- while v < 80000
- do
- insert into ti2
- values (v,'3.14',adddate('1995-01-01',(rand(v)*3652) mod 365));
- set v = v + 1;
- end while;
- end
- //
KEY 类型,代码如下:
- CREATE TABLE users (
- uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL DEFAULT '',
- email VARCHAR(30) NOT NULL DEFAULT ''
- )
- PARTITION BY KEY (uid) PARTITIONS 4 (
- PARTITION p0
- DATA DIRECTORY = '/data0/data'
- INDEX DIRECTORY = '/data1/idx',
- PARTITION p1
- DATA DIRECTORY = '/data2/data'
- INDEX DIRECTORY = '/data3/idx',
- PARTITION p2
- DATA DIRECTORY = '/data4/data'
- INDEX DIRECTORY = '/data5/idx',
- PARTITION p3
- DATA DIRECTORY = '/data6/data'
- INDEX DIRECTORY = '/data7/idx'
- );
分成4个区,数据文件和索引文件单独存放.
子分区:子分区是针对 RANGE/LIST 类型的分区表中每个分区的再次分割,再次分割可以是 HASH/KEY 等类型,例如,代码如下:
- CREATE TABLE users (
- uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL DEFAULT '',
- email VARCHAR(30) NOT NULL DEFAULT ''
- )
- PARTITION BY RANGE (uid) SUBPARTITION BY HASH (uid % 4) SUBPARTITIONS 2(
- PARTITION p0 VALUES LESS THAN (3000000)
- DATA DIRECTORY = '/data0/data'
- INDEX DIRECTORY = '/data1/idx',
- PARTITION p1 VALUES LESS THAN (6000000)
- DATA DIRECTORY = '/data2/data'
- INDEX DIRECTORY = '/data3/idx'
- );
对 RANGE 分区再次进行子分区划分,子分区采用 HASH 类型,或者如下代码:
- CREATE TABLE users (
- uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL DEFAULT '',
- email VARCHAR(30) NOT NULL DEFAULT ''
- )
- PARTITION BY RANGE (uid) SUBPARTITION BY KEY(uid) SUBPARTITIONS 2(
- PARTITION p0 VALUES LESS THAN (3000000)
- DATA DIRECTORY = '/data0/data'
- INDEX DIRECTORY = '/data1/idx',
- PARTITION p1 VALUES LESS THAN (6000000)
- DATA DIRECTORY = '/data2/data'
- INDEX DIRECTORY = '/data3/idx'
- );
对 RANGE 分区再次进行子分区划分,子分区采用 KEY 类型.
分区管理:
删除分区:ALERT TABLE users DROP PARTITION p0;,删除分区 p0。
重建分区,o RANGE 分区重建:ALTER TABLE users REORGANIZE PARTITION p0,p1 INTO (PARTITION p0 VALUES LESS THAN (6000000));将原来的 p0,p1 分区合并起来,放到新的 p0 分区中.
o LIST 分区重建:ALTER TABLE users REORGANIZE PARTITION p0,p1 INTO (PARTITION p0 VALUES IN(0,1,4,5,8,9,12,13));
将原来的 p0,p1 分区合并起来,放到新的 p0 分区中,o HASH/KEY 分区重建,ALTER TABLE users REORGANIZE PARTITION COALESCE PARTITION 2;
用 REORGANIZE 方式重建分区的数量变成2,在这里数量只能减少不能增加。想要增加可以用 ADD PARTITION 方法。
新增分区,o 新增 RANGE 分区:ALTER TABLE category ADD PARTITION (PARTITION p4 VALUES IN (16,17,18,19)
DATA DIRECTORY = '/data8/data'
INDEX DIRECTORY = '/data9/idx');
新增一个RANGE分区,o 新增 HASH/KEY 分区:ALTER TABLE users ADD PARTITION PARTITIONS 8;将分区总数扩展到8个.
给已有的表加上分区,代码如下:
- alter table results partition by RANGE (month(ttime))
- (PARTITION p0 VALUES LESS THAN (1),
- PARTITION p1 VALUES LESS THAN (2) , PARTITION p2 VALUES LESS THAN (3) ,
- PARTITION p3 VALUES LESS THAN (4) , PARTITION p4 VALUES LESS THAN (5) ,
- PARTITION p5 VALUES LESS THAN (6) , PARTITION p6 VALUES LESS THAN (7) ,
- PARTITION p7 VALUES LESS THAN (8) , PARTITION p8 VALUES LESS THAN (9) ,
- PARTITION p9 VALUES LESS THAN (10) , PARTITION p10 VALUES LESS THAN (11),
- PARTITION p11 VALUES LESS THAN (12),
- PARTITION P12 VALUES LESS THAN (13) );
默认分区限制分区字段必须是主键,PRIMARY KEY)的一部分,为了去除此限制.
方法1,使用ID,代码如下:
- mysql> ALTER TABLE np_pk
- -> PARTITION BY HASH( TO_DAYS(added) )
- -> PARTITIONS 4;
- ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function
- However, this statement using the id column for the partitioning column is valid, as shown here:
- mysql> ALTER TABLE np_pk
- -> PARTITION BY HASH(id)
- -> PARTITIONS 4;
- Query OK, 0 rows affected (0.11 sec)
- Records: 0 Duplicates: 0 Warnings: 0
方法2,将原有PK去掉生成新PK,代码如下:
- mysql> alter table results drop PRIMARY KEY;
- Query OK, 5374850 rows affected (7 min 4.05 sec)
- Records: 5374850 Duplicates: 0 Warnings: 0
- mysql> alter table results add PRIMARY KEY(id, ttime);
- Query OK, 5374850 rows affected (6 min 14.86 sec)
- Records: 5374850 Duplicates: 0 Warnings: 0
Tags: partition mysql分区功能
相关文章
- ·mysql数据库分区功能及实例详解(2014-09-23)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)