表结构如下,文章只有690篇。

文章表article(id,title,content)

标签表tag(tid,tag_name)

标签文章中间表article_tag(id,tag_id,article_id)

其中有个标签的tid是135,查询标签tid是135的文章列表。

690篇文章,用以下的语句查询,奇慢:

复制代码 代码示例:

select id,title from article where id in(

select article_id from article_tag where tag_id=135

)

其中这条速度很快:

复制代码 代码示例:

select article_id from article_tag where tag_id=135

查询结果是五篇文章,id为428,429,430,431,432

用下面sql来查文章也很快:

复制代码 代码示例:

select id,title from article where id in(

428,429,430,431,432

)

解决方法:

复制代码 代码示例:

select id,title from article where id in(

select article_id from (select article_id from article_tag where tag_id=135) as tbt

)

其它解决方法:(举例)

复制代码 代码示例:

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来改写一下该查询来进行优化。

网上也有文章说,使用JOIN语句的查询不一定总比使用子查询的语句快。

参考资料3:改变了对Mysql子查询的看法

http://hi.baidu.com/yzx110/blog/item/e694f536f92075360b55a92b.html

mysql手册也提到过,具体的原文在mysql文档的这个章节:

I.3. Restrictions on Subqueries

13.2.8. Subquery Syntax

摘抄:

1)关于使用IN的子查询:

Subquery optimization for IN is not as effective as for the = operator or for IN(value_list) constructs.

A typical case for poor IN subquery performance is when the subquery returns a small number of rows but the outer query returns a large number of rows to be compared to the subquery result.

The problem is that, for a statement that uses an IN subquery, the optimizer rewrites it as a correlated subquery. Consider the following statement that uses an uncorrelated subquery:

SELECT ... FROM t1 WHERE t1.a IN (SELECT b FROM t2);

The optimizer rewrites the statement to a correlated subquery:

SELECT ... FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t1.a);

If the inner and outer queries return M and N rows, respectively, the execution time becomes on the order of O(M×N), rather than O(M+N) as it would be for an uncorrelated subquery.

An implication is that an IN subquery can be much slower than a query written using an IN(value_list) construct that lists the same values that the subquery would return.

2)关于把子查询转换成join的:

The optimizer is more mature for joins than for subqueries, so in many cases a statement that uses a subquery can be executed more efficiently if you rewrite it as a join.

An exception occurs for the case where an IN subquery can be rewritten as a SELECT DISTINCT join. Example:

SELECT col FROM t1 WHERE id_col IN (SELECT id_col2 FROM t2 WHERE condition);

That statement can be rewritten as follows:

SELECT DISTINCT col FROM t1, t2 WHERE t1.id_col = t2.id_col AND condition;

But in this case, the join requires an extra DISTINCT operation and is not more efficient than the subquery

您可能感兴趣的文章:

mysql子查询的实例总结

mysql多表子查询与左连接的代码

mysql子查询使用EXISTS命令的例子

mysql子查询中EXISTS用法的例子

mysql子查询的五种方式

mysql 子查询与join性能对比

mysql子查询的用法举例

mysql update实现子查询的实例分享

in not 效率太慢_mysql in 子查询效率慢的优化方法详解相关推荐

  1. xpath 取标签下所有文字内容_对Xpath 获取子标签下所有文本的方法详解

    对Xpath 获取子标签下所有文本的方法详解 在爬虫中遇见这种怎么办 想提取名称, 但是 名称不在一个标签里 使用xpath string()方法 例如 data.xpath("string ...

  2. java 修改mysql数据库表结构_MYSQL数据库表结构优化方法详解

    摘要:这篇MySQL栏目下的"MYSQL数据库表结构优化方法详解",介绍的技术点是"mysql数据库表结构.MySQL数据库.数据库表结构.MySQL.据库表结构.数据库 ...

  3. mysql子查询效率高_mysql利用子查询效率怎么样

    我就废话不多说了,大家还是直接看代码吧~create or replace function aa1(a1 integer[],a2 bigint) returns void AS $$declare ...

  4. mycat 分表子查询_MyCat分库分表策略详解

    在我们的项目发展到一定阶段之后,随着数据量的增大,分库分表就变成了一件非常自然的事情.常见的分库分表方式有两种:客户端模式和服务器模式,这两种的典型代表有sharding-jdbc和MyCat.所谓的 ...

  5. 关联查询子查询效率简单比照

    (1)试验目的: 此次试验简单对比下子查询和关联查询的效率(不考虑索引),结果为子查询效率较高. (2)试验思路: 通过scott.emp创建lyl_emp表,并依照insert into lyl_e ...

  6. 吴敬琏:我国城市化主要问题是效率太低-吴敬琏-城市化-效率太低

    吴敬琏:我国城市化主要问题是效率太低|吴敬琏|城市化|效率太低 新华网上海10月16日电(记者 郑钧天) 国务院发展研究中心研究员吴敬琏日前在上海表示,我国城市化面临的主要问题是效率太低,以致资源不能 ...

  7. mysql数据库表子查询语句_MySQL使用子查询教程

    #MYSQL#这是我MyySQL教程的第四篇了,可能对于一些大神来说这些都是小儿科,但是我还是相信这些东西会对一些人有帮助的,本篇主要会介绍上面是子查询以及如何使用它们.大概会从,什么是子查询,利用子 ...

  8. mysql回滚用法_Mysql误操作后利用binlog2sql快速回滚的方法详解

    前言 在日常工作或者学习中,操作数据库时候难免会因为"大意"而误操作,需要快速恢复的话通过备份来恢复是不太可能的,下面这篇文章主要给大家介绍关于Mysql误操作后利用binlog2 ...

  9. vue 子级拿值_vue 父组件通过$refs获取子组件的值和方法详解

    前言 在vue项目中组件之间的通讯是很常见的问题,同时也是很重要的问题,我们大致可以将其分为三种情况: 父传子:在父组件中绑定值,在子组件中用props接收 子传父:在父组件中监听一个事件,在子组件中 ...

最新文章

  1. html json 访问工程,SpringBoot:Web项目中如何优雅的同时处理Json和Html请求的异常...
  2. 网络安全-防火墙与入侵检测系统
  3. python学习高级篇(part9)--对象的引用计数
  4. Dependency injection in .NET Core的最佳实践
  5. [JSOI2007]建筑抢修 (贪心)
  6. Gradle 2.3 发布
  7. mysql 分库分表中间件 mycat_阿里开源的分布式分库分表中间件之MyCat从入门到放弃...
  8. sqlite数据库读写在linux下的权限问题
  9. 用 js 写的 WebSocketHeartBeat,心跳检测,断线重连
  10. vmware不显示usb图标解决办法
  11. 商业世界的五大基础定律
  12. matlab(1):画图像修改曲线形状
  13. centos7加载磁盘
  14. CVPR2022 Oral:StreamYOLO-流感知实时检测器
  15. Dell intel i5 1135笔记本 ubuntu18.04无法调节屏幕亮度
  16. 网盘搜索工具整理2015
  17. 桂林游记(一):兴坪
  18. ACG图片站\python爬虫\LAMP环境
  19. Ext.form.DateField控件属性说明
  20. 西门子关于HMI画面中的报警信息的解析

热门文章

  1. Django 数据库查询优化,choices参数(数据库字段设计常见),MVC和MTV模型,多对多三种创建方式...
  2. 经典笔试题-我们在web 应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
  3. 征信报告有多重要?信用记录出现不良怎么办?
  4. python数据导出excel_Python实现数据库一键导出为Excel表格-----转载
  5. 2020_9_7 每日一题 前 K 个高频元素
  6. LTSPICE使用教程1:MOS管Miller效应仿真
  7. 3DMarkVantage 解析
  8. 90后ACE成长记——从偏居一隅小城里走出的核心技术人...
  9. 从零开始的openGL--cs游戏(13) 完成cs游戏的第一阶段,完成模型载入和动画,下一步做成阴影
  10. 火车到站查询api 站到站查询数据