位置: 编程技术 - 正文

mysql in语句子查询效率慢的优化技巧示例(mysql数据查询语句)

编辑:rootadmin

推荐整理分享mysql in语句子查询效率慢的优化技巧示例(mysql数据查询语句),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:mysqljoin查询,mysql查询语句菜鸟教程,mysql in查询语句,mysql查询语句菜鸟教程,mysql数据查询语句,mysql查询语句大全及用法,mysql in语句子查询效率慢的优化技巧示例,mysql用in查询,内容如对您有帮助,希望把文章链接给更多的朋友!

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

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

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

其中这条速度很快:

查询结果是五篇文章,id为,,,,

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

解决方法:

其它解决方法:(举例)

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

rows in set (. sec)

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

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

从上面的输出可以看出,这两张表在number_id字段上创建了索引的。看看子查询本身有没有问题。

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

查询出来看看:

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

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

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

参考资料1:MySQL优化之使用连接(join)代替子查询

参考资料2:MYSQL子查询和嵌套查询优化实例解析

mysql in语句子查询效率慢的优化技巧示例(mysql数据查询语句)

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

修改后:

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

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

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

mysql手册也提到过,具体的原文在mysql文档的这个章节:I.3. Restrictions on Subqueries.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

总结

本文链接地址:https://www.jiuchutong.com/biancheng/348366.html 转载请保留说明!

上一篇:浅谈mysql的子查询联合与in的效率(mysql的子查询语句)

下一篇:MYSQL子查询和嵌套查询优化实例解析(mysql子查询嵌套查询)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络