您的位置:首页 > 数据库 > MySQL

mysql not in 或in 优化

2016-01-22 15:42 603 查看
参考 /detail/2554613063.html

请允许我复制下原文

在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: