mysql 5.1与5.6 子查询性能对比
2014-08-22 12:59
274 查看
今天下午想mysql 5.1与5.6子查询性能,发现性能差距是相当大
本测试是基于两台一样的虚拟机,通过对其中一台clone来创建的虚拟机。
源表数据使用sysbench生成50000条数据,两个库数据完全一样。
mysql>select @@version
->;
+----------------------+
|@@version |
+----------------------+
|5.1.73-community-log|
+----------------------+
1rowinset(0.00sec)
mysql>select count(*) from sbtest;
ERROR1046(3D000): No database selected
mysql>use sysbench
Database changed
mysql>select count(*) from sbtest;
+----------+
|count(*)|
+----------+
| 50000|
+----------+
1rowinset(0.02sec)
mysql>create table idtable as select id from sbtest where id<=1000;
QueryOK,1000rowsaffected(0.01sec)
Records:1000 Duplicates:0 Warnings:0
mysql>select count(*) from sbtest where id in(select id from idtable);
+----------+
|count(*)|
+----------+
| 1000|
+----------+
1 row in set(35.60sec)
mysql>create index id_st on sbtest(id);
QueryOK,50000 rows affected(1.64sec)
Records:50000 Duplicates:0 Warnings:0
mysql>select count(*) from sbtest where id in(select id from idtable);
+----------+
|count(*)|
+----------+
| 1000|
+----------+
1 row in set(34.96sec)
所有测试表的数据完全一样,性能差距不是一般的大
查看两边执行计划
5.1
mysql>explain select count(*) from sbtest where id in(select id from idtable);
+----+--------------------+---------+-------+---------------+------+---------+------+-------+--------------------------+
|id|select_type |table |type |possible_keys|key |key_len|ref |rows |Extra |
+----+--------------------+---------+-------+---------------+------+---------+------+-------+--------------------------+
| 1|PRIMARY |sbtest |index|NULL |k |4 |NULL|50088|Using where;Using index|
| 2|DEPENDENT SUBQUERY|idtable|ALL |NULL |NULL|NULL |NULL| 1000|Using where |
+----+--------------------+---------+-------+---------------+------+---------+------+-------+--------------------------+
2rowsinset(0.00sec)
5.6
mysql>select * from sbtest a join(select id from idtable where id=78)b ona.id=b.id;
+----+---+---+----------------------------------------------------+----+
|id|k|c|pad |id|
+----+---+---+----------------------------------------------------+----+
|78|0| |qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt|78|
+----+---+---+----------------------------------------------------+----+
1 row inset(0.01sec)
MySQL 5.1中子查询性能是相当差,在5.6之前的版本,子查询仅仅被看成一个功能 ,生产环境完全不宜使用。在5.6之后针对这个问题进行了优化,性能有了相当大的提升,因此在5.5之前不建议使用子查询。
将5.1的in 子查询改为join 性能会有很大提升。
从以上测试来看查询时间由原来的47.51s提升至0.01s。
在5.0,5.1中如果使用子查询,尽量使用join吧。性能问题不容忽视。
本测试是基于两台一样的虚拟机,通过对其中一台clone来创建的虚拟机。
源表数据使用sysbench生成50000条数据,两个库数据完全一样。
mysql>select @@version
->;
+----------------------+
|@@version |
+----------------------+
|5.1.73-community-log|
+----------------------+
1rowinset(0.00sec)
mysql>select count(*) from sbtest;
ERROR1046(3D000): No database selected
mysql>use sysbench
Database changed
mysql>select count(*) from sbtest;
+----------+
|count(*)|
+----------+
| 50000|
+----------+
1rowinset(0.02sec)
mysql>create table idtable as select id from sbtest where id<=1000;
QueryOK,1000rowsaffected(0.01sec)
Records:1000 Duplicates:0 Warnings:0
mysql>select count(*) from sbtest where id in(select id from idtable);
+----------+
|count(*)|
+----------+
| 1000|
+----------+
1 row in set(35.60sec)
mysql>create index id_st on sbtest(id);
QueryOK,50000 rows affected(1.64sec)
Records:50000 Duplicates:0 Warnings:0
mysql>select count(*) from sbtest where id in(select id from idtable);
+----------+
|count(*)|
+----------+
| 1000|
+----------+
1 row in set(34.96sec)
mysql>select @@version ->; +------------+ |@@version | +------------+ |5.6.17-log| +------------+ 1 row in set(0.00sec) mysql>select count(*) from sbtest; +----------+ |count(*)| +----------+ | 50000| +----------+ 1rowinset(0.02sec) mysql>create table idtable as select id from sbtest where id<=1000; QueryOK,1000 rows affected(0.09sec) Records:1000 Duplicates:0 Warnings:0 mysql>select count(*) from sbtest where id in(select id from idtable); +----------+ |count(*)| +----------+ | 1000| +----------+ 1 row in set(0.00sec)
所有测试表的数据完全一样,性能差距不是一般的大
查看两边执行计划
5.1
mysql>explain select count(*) from sbtest where id in(select id from idtable);
+----+--------------------+---------+-------+---------------+------+---------+------+-------+--------------------------+
|id|select_type |table |type |possible_keys|key |key_len|ref |rows |Extra |
+----+--------------------+---------+-------+---------------+------+---------+------+-------+--------------------------+
| 1|PRIMARY |sbtest |index|NULL |k |4 |NULL|50088|Using where;Using index|
| 2|DEPENDENT SUBQUERY|idtable|ALL |NULL |NULL|NULL |NULL| 1000|Using where |
+----+--------------------+---------+-------+---------------+------+---------+------+-------+--------------------------+
2rowsinset(0.00sec)
5.6
mysql>select * from sbtest a join(select id from idtable where id=78)b ona.id=b.id;
+----+---+---+----------------------------------------------------+----+
|id|k|c|pad |id|
+----+---+---+----------------------------------------------------+----+
|78|0| |qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt|78|
+----+---+---+----------------------------------------------------+----+
1 row inset(0.01sec)
MySQL 5.1中子查询性能是相当差,在5.6之前的版本,子查询仅仅被看成一个功能 ,生产环境完全不宜使用。在5.6之后针对这个问题进行了优化,性能有了相当大的提升,因此在5.5之前不建议使用子查询。
将5.1的in 子查询改为join 性能会有很大提升。
mysql>select * from sbtest where id in(select id from idtable where id=78); +----+---+---+----------------------------------------------------+ |id|k|c|pad | +----+---+---+----------------------------------------------------+ |78|0| |qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt| +----+---+---+----------------------------------------------------+ 1 row in set(47.51sec) mysql>select * from sbtest a join(select id from idtable where id=78)b ona.id=b.id; +----+---+---+----------------------------------------------------+----+ |id|k|c|pad |id| +----+---+---+----------------------------------------------------+----+ |78|0| |qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt|78| +----+---+---+----------------------------------------------------+----+ 1 row in set(0.01sec)
从以上测试来看查询时间由原来的47.51s提升至0.01s。
在5.0,5.1中如果使用子查询,尽量使用join吧。性能问题不容忽视。
相关文章推荐
- MySQL 5.1 与 MySQL 5.5 性能测试对比
- Solr与MySQL查询性能对比
- MySQL查询随机数据的4种方法和性能对比
- MySQL查询随机数据的4种方法和性能对比
- MySQL 5.6 not in 和表关联性能测试对比
- php+mysql预查询prepare 与普通查询的性能对比
- MySQL 5.6 GA 与 MySQL 5.5 性能对比
- Solr与MySQL查询性能对比
- 关于在MySQL中查询多边形包含点情况(5.1中可用),5.6可用ST_Contains
- Solr与MySQL查询性能对比
- Solr与MySQL查询性能对比
- MySQL查询随机数据的4种方法和性能对比
- MySQL查询随机数据的4种方法和性能对比
- lucene3.5与mysql 查询性能的对比
- MySQL5.5与MySQL5.1性能对比测试
- mysql查缺补漏(二)mysql5.6性能优化(explain执行计划术语,索引,优化查询)
- Mysql5.5读写性能比5.1提高3倍多
- MYSQL笔记(查询优化续2--查询优化限制5.1版本)
- mysql中标量子查询和关联子查询的性能比较
- SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比