EXSITS应该怎么用?

无论是做项目还是普通使用SQL,我们通常都会使用IN。因为很好理解,也很方便。但是,面对着多层查询嵌套,或者IN关键字里面的结果集数量巨大,查询的效率就会直线下降。这时候,我们应该用好EXSITS。

首先,来一个简单的例子。

with score(id,name,subject,score)
as(
select 0,'张三','数学',88 from dual union all
select 1,'张三','英语',78 from dual union all
select 2,'李四','数学',68 from dual union all
select 3,'李四','英语',98 from dual union all
select 4,'王五','数学',77 from dual union all
select 5,'王五','英语',92 from dual union all
select 6,'赵六','数学',81 from dual union all
select 7,'赵六','英语',75 from dual
)
,has_phone(name,has_phone)
as(
select '张三','有' from dual union all
select '李四','没有' from dual union all
select '王五','没有' from dual union all
select '赵六','有' from dual
)
--select *
-- from score a
-- where name in (select name from has_phone where has_phone = '有')
select *
from score a
where exists (select 1
from has_phone b
where b.name = a.name
and has_phone = '有')

这段SQL的意思是,查询有手机的同学的成绩。

那么我来理解一下IN和EXSITS的区别吧。
在使用IN的时候。
数据库首先是去在has_phone里面查找所有条件为“有”的name。
然后把这些结果集让每一个name去匹配。

在使用EXSITS的时候。
数据库是先查询SCORE,然后每个连接条件到EXSITS里面进行判断。
如果为TRUE,则加入结果集,否则就跳过。

EXSITS执行过程
可以理解为:
for x in (select * from score a)
loop

if(exists(select 1 from has_phone b where b.name = a.name ))
then
output the record;
end if;

end loop;

对于in 和 exists的性能区别:
如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in;
反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists.
其实我们区分in和exists主要是造成了驱动顺序的改变(这是性能变化的关键),如果是exists,
那么以外层表为驱动表,先被访问,如果是IN,那么先执行子查询,所以我们会以驱动表的快速返回为目标,
那么就会考虑到索引及结果集的关系了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值