mysql里join in,为什么MySQL JOIN显着快于WHERE IN(子查询)

I am trying to better understand why this query optimization is so significant (over 100 times faster) so I can reuse similar logic for other queries.

Using MySQL 4.1 - RESET QUERY CACHE and FLUSH TABLES was done before all queries and result time can be reproduced consistently. Only thing that is obvious to me on the EXPLAIN is that only 5 rows have to be found during the JOIN ? But is that the whole answer to the speed? Both queries are using a partial index (forum_stickies) to determine deleted topics status (topic_status=0)

Screenshots for deeper analysis with EXPLAIN

slow query: 0.7+ seconds (cache cleared)

SELECT SQL_NO_CACHE forum_id, topic_id FROM bb_topics

WHERE topic_last_post_id IN

(SELECT SQL_NO_CACHE MAX (topic_last_post_id) AS topic_last_post_id

FROM bb_topics WHERE topic_status=0 GROUP BY forum_id)

fast query: 0.004 seconds or less (cache cleared)

SELECT SQL_NO_CACHE forum_id, topic_id FROM bb_topics AS s1

JOIN

(SELECT SQL_NO_CACHE MAX(topic_last_post_id) AS topic_last_post_id

FROM bb_topics WHERE topic_status=0 GROUP BY forum_id) AS s2

ON s1.topic_last_post_id=s2.topic_last_post_id

Note there is no index on the most important column (topic_last_post_id) but that cannot be helped (results are stored for repeated use anyway).

Is the answer simply because the first query has to scan topic_last_post_id TWICE, the second time to match up the results to the subquery? If so, why is it exponentially slower?

(less important I am curious why the first query still takes so long if I actually do put an index on topic_last_post_id)

update: I found this thread on stackoverflow after much searching later on which goes into this topic Subqueries vs joins

解决方案

Maybe the engine executes the subquery for every row in bb_topics, just to see if it finds the topic_last_post_id in the results. Would be stupid, but would also explain the huge difference.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值