mysql> select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');

为了节省篇幅,省略了输出内容,下同。

67 rows in set (12.00 sec)

只有67行数据返回,却花了12秒,而系统中可能同时会有很多这样的查询,系统肯定扛不住。用desc看一下(注:explain也可)

mysql> desc select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');
+----+--------------------+------------------+--------+-----------------+-------+---------+------------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------------+--------+-----------------+-------+---------+------------+---------+--------------------------+
| 1 | PRIMARY | abc_number_prop | ALL | NULL | NULL | NULL | NULL | 2679838 | Using where |
| 2 | DEPENDENT SUBQUERY | abc_number_phone | eq_ref | phone,number_id | phone | 70 | const,func | 1 | Using where; Using index |
+----+--------------------+------------------+--------+-----------------+-------+---------+------------+---------+--------------------------+
2 rows in set (0.00 sec)

从上面的信息可以看出,在执行此查询时会扫描两百多万行,难道是没有创建索引吗,看一下

mysql>show index from abc_number_phone;
+------------------+------------+-------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------------+------------+-------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| abc_number_phone | 0 | PRIMARY | 1 | number_phone_id | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 0 | phone | 1 | phone | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 0 | phone | 2 | number_id | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 1 | number_id | 1 | number_id | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 1 | created_by | 1 | created_by | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 1 | modified_by | 1 | modified_by | A | 36879 | NULL | NULL | YES | BTREE | | |
+------------------+------------+-------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
6 rows in set (0.06 sec)

mysql>show index from abc_number_prop;
+-----------------+------------+-------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------+------------+-------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| abc_number_prop | 0 | PRIMARY | 1 | number_prop_id | A | 311268 | NULL | NULL | | BTREE | | |
| abc_number_prop | 1 | number_id | 1 | number_id | A | 311268 | NULL | NULL | | BTREE | | |
| abc_number_prop | 1 | created_by | 1 | created_by | A | 311268 | NULL | NULL | | BTREE | | |
| abc_number_prop | 1 | modified_by | 1 | modified_by | A | 311268 | NULL | NULL | YES | BTREE | | |
+-----------------+------------+-------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.15 sec)

从上面的输出可以看出,这两张表在number_id字段上创建了索引的。

看看子查询本身有没有问题。

mysql> desc select number_id from abc_number_phone where phone = '82306839';
+----+-------------+------------------+------+---------------+-------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+------+---------------+-------+---------+-------+------+--------------------------+
| 1 | SIMPLE | abc_number_phone | ref | phone | phone | 66 | const | 6 | Using where; Using index |
+----+-------------+------------------+------+---------------+-------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

没有问题,只需要扫描几行数据,索引起作用了。查询出来看看

mysql> select number_id from abc_number_phone where phone = '82306839';
+-----------+
| number_id |
+-----------+
| 8585 |
| 10720 |
| 148644 |
| 151307 |
| 170691 |
| 221897 |
+-----------+
6 rows in set (0.00 sec)

直接把子查询得到的数据放到上面的查询中

mysql> select * from abc_number_prop where number_id in (8585, 10720, 148644, 151307, 170691, 221897);

67 rows in set (0.03 sec)

速度也快,看来MySQL在处理子查询的时候是不够好。我在MySQL 5.1.42 和 MySQL 5.5.19 都进行了尝试,都有这个问题。

搜索了一下网络,发现很多人都遇到过这个问题:

参考资料1:使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录
http://blog.csdn.net/hongsejiaozhu/article/details/1876181
参考资料2:网站开发日记(14)-MYSQL子查询和嵌套查询优化
http://dodomail.iteye.com/blog/250199

根据网上这些资料的建议,改用join来试试。

修改前:select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');

修改后:select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';

mysql> select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';

67 rows in set (0.00 sec)

效果不错,查询所用时间几乎为0。看一下MySQL是怎么执行这个查询的

mysql>desc select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';
+----+-------------+-------+------+-----------------+-----------+---------+-----------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-----------------+-----------+---------+-----------------+------+--------------------------+
| 1 | SIMPLE | b | ref | phone,number_id | phone | 66 | const | 6 | Using where; Using index |
| 1 | SIMPLE | a | ref | number_id | number_id | 4 | eap.b.number_id | 3 | |
+----+-------------+-------+------+-----------------+-----------+---------+-----------------+------+--------------------------+
2 rows in set (0.00 sec)

小结:当子查询速度慢时,可用JOIN来改写一下该查询来进行优化。

mysql in 查询效率慢优化相关推荐

  1. Mysql中查询速度的优化

    2019独角兽企业重金招聘Python工程师标准>>> mysql中查询速度的优化 1.查看每一个表的数据量 2.left join的时候尽量减少表的大小,通过临时表,查询条件去限制 ...

  2. mysql模糊查询提速_【MySQL】Mysql模糊查询like提速优化

    [MySQL]Mysql模糊查询like提速优化 在使用msyql进行模糊查询的时候,很自然的会用到like语句,通常情况下,在数据量小的时候,不容易看出查询的效率,但在数据量达到百万级,千万级的时候 ...

  3. 在左表或右表的连接字段上建立索引对左、内连接的查询效率的优化情况分析

    -- 1.建表 drop table if exists dept_tbl;create table dept_tbl (rcrd_id int unsigned primary key auto_i ...

  4. MySQL分页查询方法及优化

    MySQL分页查询方法及优化 http://uusama.com/458.html

  5. 提高MySQL数据库查询效率的几个技巧(转载)

    [size=5][color=Red]提高MySQL数据库查询效率的几个技巧(转)[/color][/size]       MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用. ...

  6. mysql模糊查询之索引优化

    mysql模糊查询之索引优化 三表连查之模糊查询索引优化 注:具体用法10和11步骤. 1.t_project表 DROP TABLE IF EXISTS `t_project`; CREATE TA ...

  7. mysql分页查询关键_MySQL优化教程之超大分页查询

    背景 基本上只要是做后台开发,都会接触到分页这个需求或者功能吧.基本上大家都是会用MySQL的LIMIT来处理,而且我现在负责的项目也是这样写的.但是一旦数据量起来了,其实LIMIT的效率会极其的低, ...

  8. mysql的查询语句怎么优化_MySQL查询语句如何优化

    前言查询语句的优化是SQL效率优化的一个方式,可以通过优化sql语句来尽量使用已有的索引,避免全表扫描,从而提高查询效率.最近在对项目中的一些sql进行优化,总结整理了一些方法. 1.尽量避免在 wh ...

  9. mongo mysql 条件查询效率_mongodb查询条件对查询效率的影响

    背景和概述 数据状况: mongodb的一个db中存了近2000万记录,有type(字符串).ctime(float,创建时间)等索引参数,记录的自然顺序和ctime的排序是一致的. 查询需求: 从第 ...

最新文章

  1. arcgis java api_Arcgis API For Javascript下载与安装
  2. form表单提交数据编码方式和tomcat接受数据解码方式
  3. 吐血整理!这可能是最全的机器学习工具手册
  4. 技术解读|云上企业级存储——打开存储新维度,促进用户核心业务创新
  5. 机器学习速成课程 | 练习 | Google Development——编程练习:使用 TensorFlow 的起始步骤
  6. Asynchronous(异步处理)
  7. echarts无数据时显示暂无数据或者用图片代替
  8. 服务器托管过程中勒索病毒的预防
  9. CC1110使用433MHz收发数据
  10. 【UE4 附源工程】VR直升机模拟飞行与轰炸制作流程
  11. 一个前端搞淘宝模板那些事
  12. html video标签canplay,HTML oncanplay事件用法及代码示例
  13. 什么是AJAX四步?分别是?(AJAX的使用)
  14. vue中实现汉字转化拼音
  15. 11.30黄金伦敦金还会跌吗?黄金原油独家交易指导及策略解套
  16. 震惊!十六岁少女竟然被三名阿里p8老师讲解{常见面试题汇总}
  17. 计算机教师培训内容,(计算机教师培训内容.doc
  18. aspectj weaver记录
  19. RTSP协议解析-概述
  20. 二重积分转换成极坐标_二重积分转换公式注意将直角坐标系的二重积分化为极坐标.PPT...

热门文章

  1. 《过故人庄》古诗鉴赏
  2. 连詹姆斯·高斯林(JAVA之父)都要被气疯掉的JAVA代码注释
  3. MySQL-5.6版本GTID的主从复制
  4. EventBus、Rxjava、RxBus的定义、作用、使用方式及区别
  5. string类型 的 push_back()
  6. Python 语言中有关复数的几个概念
  7. 腾讯QQ企业邮箱设置
  8. 【IoT】硬件PM系列(三):硬件产品经理需要掌握的定价策略
  9. mpc5125 开发板 基于 核心板
  10. OpenCV之阈值化操作总结