mysql not in 或in 优化

原创
2016/01/22 15:42
阅读数 1.4K

参考 http://blog.csdn.net/aeolus_pu/article/details/7800699

请允许我复制下原文

在mysql 中,not in 或in 优化思路, 利用left join 来优化,类似如下的查询方式 
          select id  from  a  where id  in  (select  id from b   )  
        如这样的查询方式,在大数据量的情况下,查询很慢,需要改写优化sql,那么就可以用left join来优化改写如下格式:
   select id from a left join b on a.id =b.id  where b.id is not null 
 其实优化思想就是利用join 连接,提高效率 。

not in 与其相反:

select id form a left join b on a.id = b.id where b.id is null

利用left join 查询 得出的结果集是a的全集以及a,b之间id相等的集合。此时条件b的id 是 not null的,那么a的id肯定在b中存在等同于查询语句中的in,反之,b的id是null的,a的id肯定不再b中等同于 not in 

展开阅读全文
加载中

作者的其它热门文章

打赏
1
0 收藏
分享
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部