Mysql嵌套查询与连接查询的效率问题

执行语句如下:select count(DISTINCT l1) from t1 where l1 in(select l2 from t2 where l3='xxx') 感觉速度很慢(几十秒),

但是单独执行子查询select l2 from t2 where l3='xxx'或者去掉子查询这个直接执行select count(DISTINCT l1) from t1都比较快。

将语句改成join的形式效果就快很多(毫秒级别),修改后语句如下:select  count(DISTINCT l1) from t1 inner join t2 where t1.l1=t2.l2 and t2.l3='xxx'。

具体原因分析可见:https://www.cnblogs.com/Bccd/p/6607515.html

posted @ 2017-11-16 18:25  Edchen  阅读(10098)  评论(0)    收藏  举报