not in & not exists

 通过使用EXISTS,Oracle系统会首先检查主查询,然后运行子查询直到找到第一个匹配项,这就节省了时间。Oracle系统在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。

  同时应尽可能使用NOT EXISTS来代替NOT IN,尽管二者都使用了NOT(不能使用索引而降低速度),但NOT EXISTS要比NOT IN查询效率更高。

以下的null代表真的null,写在这里只是为了让大家看清楚


根据如下表的查询结果,那么以下语句的结果是(知识点:not in/not exists+null)
SQL> select * from usertable;
USERID           USERNAME
-----------      ----------------
     1          user1
     2          null
     3          user3
     4          null
     5          user5
     6          user6
      
SQL> select * from usergrade;
USERID         USERNAME           GRADE
----------     ----------------   ----------
     1        user1              90
     2        null               80
     7        user7              80
     8        user8              90
执行语句:

select count(*) from usergrade where username not in (select username from usertable);

select count(*) from usergrade g where not exists
(select null from usertable t where t.userid=g.userid and t.username=g.username);

结果为:语句1(  0 )   语句2  (  3 )

A: 0     B:1     C:2     D:3      E:NULL


2

在以下的表的显示结果中,以下语句的执行结果是(知识点:in/exists+rownum)
SQL> select * from usertable;
USERID           USERNAME
-----------      ----------------
     1          user1
     2          user2
     3          user3
     4          user4
     5          user5
      
SQL> select * from usergrade;
USERNAME               GRADE
----------------       ----------
user9                  90
user8                  80
user7                  80
user2                  90
user1                  100
user1                  80

执行语句
Select count(*) from usertable t1 where username in
  (select username from usergrade t2 where rownum <=1);
  
Select count(*) from usertable t1 where exists
  (select 'x' from usergrade t2 where t1.username=t2.username and rownum <=1);

以上语句的执行结果是:(  )  (  )
  A:   0        B:   1        C:   2       D:  3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值