当前位置:Gxlcms > 数据库问题 > sql中in和exists效率问题 转自百度知道

sql中in和exists效率问题 转自百度知道

时间:2021-07-01 10:21:17 帮助过:2人阅读

* from A where cc in (select cc from B)

效率低,用到了A表上cc列的索引;

 

select * from A where exists(select cc from B where cc=A.cc)

效率高,用到了B表上cc列的索引。


相反的2:

select * from B where cc in (select cc from A)

效率高,用到了B表上cc列的索引;

 

select * from B where exists(select cc from A where cc=B.cc)

效率低,用到了A表上cc列的索引。


not in 和not exists如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。

所以无论那个表大,用not exists都比not in要快

 

in 与 =的区别

select name from student where name in (zhang,wang,li,zhao);

select name from student where name=zhang or name=li or
name=wang or name=zhao

的结果是相同的。

 

最近一直在写sql语句,用到了in函数,百度知道看到了大神的回答,受益良多,转载至此。

人气教程排行