您的位置:首页 > 数据库 > MySQL

表分区MySQL版本:5.5.11比5.1查询速度明显提高(4倍左右)

2013-06-03 15:09 579 查看
实验1

1、建表:

create table `t_part_test`(
`id` int NOT NULL default 0,
`detail` char(32) NOT NULL default '',
primary key (id)
)default charset=gbk
PARTITION BY HASH(id) PARTITIONS 10;

create table `t_nopart`(
`id` int NOT NULL default 0,
`detail` char(32) NOT NULL default '',
primary key (id)
)default charset=gbk;

CREATE TABLE `t_part_test_range` (
`id` int(11) NOT NULL DEFAULT '0',
`detail` char(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
PARTITION BY RANGE (id)
(PARTITION p01 VALUES LESS THAN (1000000),
PARTITION p02 VALUES LESS THAN (2000000),
PARTITION p03 VALUES LESS THAN (3000000),
PARTITION p04 VALUES LESS THAN (4000000),
PARTITION p05 VALUES LESS THAN (5000000),
PARTITION p06 VALUES LESS THAN (6000000),
PARTITION p07 VALUES LESS THAN (7000000),
PARTITION p08 VALUES LESS THAN (8000000),
PARTITION p09 VALUES LESS THAN (9000000),
PARTITION p10 VALUES LESS THAN MAXVALUE);

注意:5.5默认存储引擎是InnoDB

2、向三张表中分别注入10000000条数据

mysql> select count(*) from t_nopart;
+----------+
| count(*) |
+----------+
| 10000000 |
+----------+
1 row in set (4.68 sec)

mysql> select count(*) from t_part_test;
+----------+
| count(*) |
+----------+
| 10000000 |
+----------+
1 row in set (3.89 sec)

mysql> select count(*) from t_part_test_range;
+----------+
| count(*) |
+----------+
| 10000000 |
+----------+
1 row in set (3.15 sec)

3、进行查询实验

mysql> select count(*) from t_nopart where id>=3000000 and id<=8099900;
+----------+
| count(*) |
+----------+
| 5099901 |
+----------+
1 row in set (3.33 sec)

mysql> select count(*) from t_part_test where id>=3000000 and id<=8099900;

+----------+
| count(*) |
+----------+
| 5099901 |
+----------+
1 row in set (1.83 sec)

mysql> select count(*) from t_part_test_range where id>=3000000 and id<=8099900;
+----------+
| count(*) |
+----------+
| 5099901 |
+----------+
1 row in set (1.73 sec)

可见分区比不分区的查询性能提高45%

4、增加非关联字段

mysql> select count(*) from t_nopart where id>=1111111 and id<=6788877 and detail like'%10%';
+----------+
| count(*) |
+----------+
| 273065 |
+----------+
1 row in set (4.74 sec)

mysql> select count(*) from t_part_test where id>=1111111 and id<=6788877 and detail like'%10%';
+----------+
| count(*) |
+----------+
| 273065 |
+----------+
1 row in set (4.22 sec)

mysql> select count(*) from t_part_test_range where id>=1111111 and id<=6788877 and detail like'%10%';
+----------+
| count(*) |
+----------+
| 273065 |
+----------+
1 row in set (3.62 sec)

hash分区提高11%

range分区提高31%

mysql> select * from t_nopart where id>=2000000 and id<=8000000 and detail='第5546328条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 5546328 | 第5546328条记录 |
+---------+-----------------+
1 row in set (4.16 sec)

mysql> select * from t_part_test where id>=2000000 and id<=8000000 and detail='第5546328条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 5546328 | 第5546328条记录 |
+---------+-----------------+
1 row in set (5.32 sec)

mysql> select * from t_part_test_range where id>=2000000 and id<=8000000 and detail='第5546328条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 5546328 | 第5546328条记录 |
+---------+-----------------+
1 row in set (3.61 sec)

hash分区性能降低

range性能提高

5、查询字段为非索引、非分区关联字段

mysql> select * from t_nopart where detail='第5555876条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 5555876 | 第5555876条记录 |
+---------+-----------------+
1 row in set (6.89 sec)

mysql> select * from t_part_test where detail='第5555876条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 5555876 | 第5555876条记录 |
+---------+-----------------+
1 row in set (11.26 sec)

mysql> select * from t_part_test_range where detail='第5555876条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 5555876 | 第5555876条记录 |
+---------+-----------------+
1 row in set (5.47 sec)

同上

实验2

对无主键的表实验:

1、建表

CREATE TABLE `t_nopri_nopart` (
`id` int(11) NOT NULL DEFAULT '0',
`detail` char(32) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=gbk;

CREATE TABLE `t_nopri_part` (
`id` int(11) NOT NULL DEFAULT '0',
`detail` char(32) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=gbk
PARTITION BY RANGE (id)
(PARTITION p01 VALUES LESS THAN (1000000),
PARTITION p02 VALUES LESS THAN (2000000),
PARTITION p03 VALUES LESS THAN (3000000),
PARTITION p04 VALUES LESS THAN (4000000),
PARTITION p05 VALUES LESS THAN (5000000),
PARTITION p06 VALUES LESS THAN (6000000),
PARTITION p07 VALUES LESS THAN (7000000),
PARTITION p08 VALUES LESS THAN (8000000),
PARTITION p09 VALUES LESS THAN (9000000),
PARTITION p10 VALUES LESS THAN MAXVALUE);

2、向两个表中个注入500万条数据

mysql> select count(*) from t_nopri_part;
+----------+
| count(*) |
+----------+
| 5000000 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from t_nopri_nopart;
+----------+
| count(*) |
+----------+
| 5000000 |
+----------+
1 row in set (0.00 sec)

3、查询测试

mysql> select count(*) from t_nopri_part where id between 2200000 and 8000000;
+----------+
| count(*) |
+----------+
| 2800001 |
+----------+
1 row in set (1.04 sec)

mysql> select count(*) from t_nopri_nopart where id between 2200000 and 8000000;
+----------+
| count(*) |
+----------+
| 2800001 |
+----------+
1 row in set (1.68 sec)

速度提高大约40%

4、增加非关联字段

mysql> select count(*) from t_nopri_part where id>=1000001 and id<=3000000 and detail like'%10%';
+----------+
| count(*) |
+----------+
| 194831 |
+----------+
1 row in set (0.94 sec)

mysql> select count(*) from t_nopri_nopart where id>=1000001 and id<=3000000 and detail like'%10%';
+----------+
| count(*) |
+----------+
| 194831 |
+----------+
1 row in set (1.17 sec)

分区表速度提高20%

mysql> select count(*) from t_nopri_part where id>=1500000 and id<=3600000 and detail='第2899999条记录';
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.51 sec)

mysql> select count(*) from t_nopri_nopart where id>=1500000 and id<=3600000 and detail='第2899999条记录';
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.79 sec)

分区表速度提高35%

5、查询条件只有分关联字段

mysql> select * from t_nopri_part where detail='第3889897条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 3889897 | 第3889897条记录 |
+---------+-----------------+
1 row in set (0.79 sec)
mysql> select * from t_nopri_nopart where detail='第3889897条记录';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 3889897 | 第3889897条记录 |
+---------+-----------------+
1 row in set (0.75 sec)

无明显区别

6、查询条件只有分区关联字段

mysql> select * from t_nopri_part where id='3888888';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 3888888 | 第3888888条记录 |
+---------+-----------------+
1 row in set (0.13 sec)

mysql> select * from t_nopri_nopart where id='3888888';
+---------+-----------------+
| id | detail |
+---------+-----------------+
| 3888888 | 第3888888条记录 |
+---------+-----------------+
1 row in set (0.53 sec)

分区速度提高75;

结论

1、相对于5.1,MySQL5.5在表分区功能上有很大改善,主要是查询性能上的明显改善;

2、分区速度的提升类似建了索引,所以如果需要分区的字段已经建了索引,一般没有必要在做分区;

3、mysql的表分区还有一个重要的功能就是可以将表的各分区放到不同的磁盘上,以增加表容量;

4、range分区比hash分区的查询性能高;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: