mysql中in和exist的区别
其他
2020-02-25 11:01:02
阅读次数: 0
mysql中in和exists的区别
-- in写法 select * from A where A.id in (select bid from B ) and A.name in (select bname from B ) ; -- exits写法 select * from A where EXISTS (select 1 from B.bid = A.id );
-
区别1
当B表的数据远大于A表的数据 exits的效率更高(B表大,用exits) 当B表的数据远小于A表的数据 in的效率更高(B表小,用in) 当B表的数据和A表的数据差不多 in/exits效率基本一致
-
区别2
-- 如果关联的字段是多个的时候用exits效率更高,可以减少B表的查询次数,提高效率 --例子:除了通过id,还需要name字段来进行关联 -- 使用in来完成会查询B表多次,效率低 select * from A where A.id in (select bid from B ) and A.name in (select bname from B ) ; -- 使用existis来完成,可以减少B表的查询次数,提高效率 select * from A where EXISTS (select 1 from B.bid = A.id and B.bname = A.name )
|
|
转载自www.cnblogs.com/zhuxiaopijingjing/p/12360434.html