mysql partition自mysql 5.1.3起开始支持分区功能。mysql表中存储的记录和表对应的索引信息,最后都是以文件的方式存储在计算机的硬盘上的,有了分区功能我们就可以做比以前更多优化了。
目前分区规则有四种,分别是RANGE、LIST、HASH和KEY,另外通过DATA DIRECTORY和INDEX DIRECTORY选项可以把不同的分区数据文件分散到不同的磁盘上,从而进步一提高系统的IO吞吐量。因此按照业务逻辑设计好了分区,可以大大提高查询效率,而且删除数据可能也会很容易。但是分区也有一些限制:1、主键或者唯一索引必须包含分区字段;2、只能通过int类型的字段或者返回int类型的表达式来分区;3、单表最多只能有1024个分区。
默认mysql是开启了分区功能的,可以通过下述查询查看结果:
- show variables like '%partition%';
- +-------------------+-------+
- | Variable_name | Value |
- +-------------------+-------+
- | have_partitioning | YES |
- +-------------------+-------+
YES 表示开启下面也range规则为例介绍一下分区常用的命令。
1、创建分区,可以在创建表的同时创建,也可以在表创建后追加分区,代码如下:
- drop table if exists `netingcn_com`;
- create table `netingcn_com` (
- `id` int(11) unsigned not null auto_increment,
- `day` int(11) not null default 0,
- primary key (`id`, `day`)
- ) engine=innodb default charset=utf8 auto_increment=1;
- alter table `netingcn_com` partition by range(`day`) (
- partition p_2012 values less than (20130000),
- partition p_2013 values less than (20140000)
- );
- --或
- drop table if exists `netingcn_com`;
- create table `netingcn_com` (
- `id` int(11) unsigned not null auto_increment,
- `day` int(11) not null default 0,
- primary key (`id`, `day`)
- ) engine=innodb default charset=utf8 auto_increment=1
- partition by range(`day`) (
- partition p_2012 values less than (20130000),
- partition p_2013 values less than (20140000)
- );
可以explain命令查看分区是否创建成功,代码如下:
- explain partitions select * from netingcn_com where day = 20130412;
- +----+-------------+--------------+------------+-------+
- | id | select_type | table | partitions | type |
- +----+-------------+--------------+------------+-------+
- | 1 | SIMPLE | netingcn_com | p_2013 | index |
- +----+-------------+--------------+------------+-------+
2、增加或删除分区,注意,删除分区的同时,该分区的所有数据也会别删除,增加分区,代码如下:
- alter table netingcn_com add partition (
- partition p_2014 values less than (20150000)
- );
- --删除分区
- alter table netingcn_com drop partition p_2012;
- --3、重新分区。注意:hash和key分区规则不能用REORGANIZE.来重新分区
- alter table netingcn_com reorganize partition p_2013,p_2014 into (partition p_2014 values less than (20150000));
[分区表和未分区表试验过程],*创建分区表,按日期的年份拆分,代码如下:
- mysql> CREATE TABLE part_tab ( c1 int default NULL, c2 varchar(30) default NULL, c3 date default NULL) engine=myisam
- PARTITION BY RANGE (year(c3)) (PARTITION p0 VALUES LESS THAN (1995),
- PARTITION p1 VALUES LESS THAN (1996) , PARTITION p2 VALUES LESS THAN (1997) ,
- PARTITION p3 VALUES LESS THAN (1998) , PARTITION p4 VALUES LESS THAN (1999) ,
- PARTITION p5 VALUES LESS THAN (2000) , PARTITION p6 VALUES LESS THAN (2001) ,
- PARTITION p7 VALUES LESS THAN (2002) , PARTITION p8 VALUES LESS THAN (2003) ,
- PARTITION p9 VALUES LESS THAN (2004) , PARTITION p10 VALUES LESS THAN (2010),
- PARTITION p11 VALUES LESS THAN MAXVALUE );
注意最后一行,考虑到可能的最大值,创建未分区表,代码如下:
mysql> create table no_part_tab (c1 int(11) default NULL,c2 varchar(30) default NULL,c3 date default NULL) engine=myisam;
通过存储过程灌入800万条测试数据,代码如下:
- mysql> set sql_mode=''; /* 如果创建存储过程失败,则先需设置此变量, bug? */
- mysql> delimiter // /* 设定语句终结符为 //,因存储过程语句用;结束 */
- mysql> CREATE PROCEDURE load_part_tab()
- begin
- declare v int default 0;
- while v < 8000000
- do
- insert into part_tab
- values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));
- set v = v + 1;
- end while;
- end
- //
- mysql> delimiter ;
- mysql> call load_part_tab();
- Query OK, 1 row affected (8 min 17.75 sec)
- mysql> insert into no_part_tab select * from part_tab;
- Query OK, 8000000 rows affected (51.59 sec)
- Records: 8000000 Duplicates: 0 Warnings: 0
测试SQL性能,代码如下:
- mysql> select count(*) from part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31';
- +----------+
- | count(*) |
- +----------+
- | 795181 |
- +----------+
- 1 row in set (0.55 sec)
- mysql> select count(*) from no_part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31';
- +----------+
- | count(*) |
- +----------+
- | 795181 |
- +----------+
- 1 row in set (4.69 sec)
结果表明分区表比未分区表的执行时间少90%.
通过explain语句来分析执行情况:
- mysql > explain select count(*) from no_part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31'G
- /* 结尾的G使得mysql的输出改为列模式 */
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: no_part_tab
- type: ALL
- possible_keys: NULL
- key: NULL
- key_len: NULL
- ref: NULL
- rows: 8000000
- Extra: Using where
- 1 row in set (0.00 sec)
- mysql> explain select count(*) from part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31'G
- *************************** 1. row ***************************
- id: 1 --Vevb.com
- select_type: SIMPLE
- table: part_tab
- type: ALL
- possible_keys: NULL
- key: NULL
- key_len: NULL
- ref: NULL
- rows: 798458
- Extra: Using where
- 1 row in set (0.00 sec)
explain语句显示了SQL查询要处理的记录数目,代码如下:
- * 试验创建索引后情况
- mysql> create index idx_of_c3 on no_part_tab (c3);
- Query OK, 8000000 rows affected (1 min 18.08 sec)
- Records: 8000000 Duplicates: 0 Warnings: 0
- mysql> create index idx_of_c3 on part_tab (c3);
- Query OK, 8000000 rows affected (1 min 19.19 sec)
- Records: 8000000 Duplicates: 0 Warnings: 0
- 创建索引后的数据库文件大小列表:
- 2008-05-24 09:23 8,608 no_part_tab.frm
- 2008-05-24 09:24 255,999,996 no_part_tab.MYD
- 2008-05-24 09:24 81,611,776 no_part_tab.MYI
- 2008-05-24 09:25 0 part_tab#P#p0.MYD
- 2008-05-24 09:26 1,024 part_tab#P#p0.MYI
- 2008-05-24 09:26 25,550,656 part_tab#P#p1.MYD
- 2008-05-24 09:26 8,148,992 part_tab#P#p1.MYI
- 2008-05-24 09:26 25,620,192 part_tab#P#p10.MYD
- 2008-05-24 09:26 8,170,496 part_tab#P#p10.MYI
- 2008-05-24 09:25 0 part_tab#P#p11.MYD
- 2008-05-24 09:26 1,024 part_tab#P#p11.MYI
- 2008-05-24 09:26 25,656,512 part_tab#P#p2.MYD
- 2008-05-24 09:26 8,181,760 part_tab#P#p2.MYI
- 2008-05-24 09:26 25,586,880 part_tab#P#p3.MYD
- 2008-05-24 09:26 8,160,256 part_tab#P#p3.MYI
- 2008-05-24 09:26 25,585,696 part_tab#P#p4.MYD
- 2008-05-24 09:26 8,159,232 part_tab#P#p4.MYI
- 2008-05-24 09:26 25,585,216 part_tab#P#p5.MYD
- 2008-05-24 09:26 8,159,232 part_tab#P#p5.MYI
- 2008-05-24 09:26 25,655,740 part_tab#P#p6.MYD
- 2008-05-24 09:26 8,181,760 part_tab#P#p6.MYI
- 2008-05-24 09:26 25,586,528 part_tab#P#p7.MYD
- 2008-05-24 09:26 8,160,256 part_tab#P#p7.MYI
- 2008-05-24 09:26 25,586,752 part_tab#P#p8.MYD
- 2008-05-24 09:26 8,160,256 part_tab#P#p8.MYI
- 2008-05-24 09:26 25,585,824 part_tab#P#p9.MYD
- 2008-05-24 09:26 8,159,232 part_tab#P#p9.MYI
- 2008-05-24 09:25 8,608 part_tab.frm
- 2008-05-24 09:25 68 part_tab.par
- * 再次测试SQL性能
- mysql> select count(*) from no_part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31'; +----------+
- | count(*) |
- +----------+
- | 795181 |
- +----------+
- 1 row in set (2.42 sec) /* 为原来4.69 sec 的51%*/
重启mysql(net stop mysql, net start mysql)后,查询时间降为0.89 sec,几乎与分区表相同,代码如下:
- mysql> select count(*) from part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31';
- +----------+
- | count(*) |
- +----------+
- | 795181 |
- +----------+
- 1 row in set (0.86 sec)
- * 更进一步的试验
- ** 增加日期范围
- mysql> select count(*) from no_part_tab where c3 > date '1995-01-01' and c3 < date '1997-12-31';
- +----------+
- | count(*) |
- +----------+
- | 2396524 |
- +----------+
- 1 row in set (5.42 sec)
- mysql> select count(*) from part_tab where c3 > date '1995-01-01' and c3 < date '1997-12-31';
- +----------+
- | count(*) |
- +----------+
- | 2396524 |
- +----------+
- 1 row in set (2.63 sec)
- ** 增加未索引字段查询
- mysql> select count(*) from part_tab where c3 > date '1995-01-01' and c3 < date
- '1996-12-31' and c2='hello';
- +----------+
- | count(*) |
- +----------+
- | 0 |
- +----------+
- 1 row in set (0.75 sec)
- mysql> select count(*) from no_part_tab where c3 > date '1995-01-01' and c3 < da
- te '1996-12-31' and c2='hello';
- +----------+
- | count(*) |
- +----------+
- | 0 |
- +----------+
- 1 row in set (11.52 sec)
= 初步结论 =
* 分区和未分区占用文件空间大致相同,数据和索引文件.
* 如果查询语句中有未建立索引字段,分区时间远远优于未分区时间
* 如果查询语句中字段建立了索引,分区和未分区的差别缩小,分区略优于未分区.
新闻热点
疑难解答