exist和in的区别原创
金蝶云社区-希有
希有
18人赞赏了该文章 1,227次浏览 未经作者许可,禁止转载编辑于2021年06月15日 14:04:46

exist和in的区别:


select * from a where id in(select id from  b);    

select * from a where exists(select 1 from b where id=a.id);


差别较大;

    使用in ,sql语句是先执行子查询,也就是先查询b表,在查a表,

    而使用exists是先查主表a ,再查字表b; 

    对于主表数据较多时,我们使用in速度比exist更快,反之,从表b较大时,使用exist插叙速度更快(都会使用索引),如果使用的是not in与not exists,直接使用not exists,因为not in 会进行全表扫描不走索引,not exists会走索引。


赞 18