sql中exists和in的区别
exists和in
in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。
如:
A:select * from t1 a where exists (select * from t2 b where b.id = a.id)
B:select * from t1 a where a.id in (select b.id from t2 b)
对于A,用到了t2上的id索引,exists执行次数为t1.length,不缓存exists的结果集。
对于B,用到了t1上的id索引,首先执行in语句,然后将结果缓存起来,之后遍历t1表,将满足结果的加入结果集,所以执行次数为t1.length*t2.length次。
因此对t1表大t2表小的情况使用in,t2表小t1表大的情况使用exists
**不管t1和t2大小如何,均使用exists效率会更高。**