阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Oracle exists/in和not exists/not in之前的区别与联系

135次阅读
没有评论

共计 3370 个字符,预计需要花费 9 分钟才能阅读完成。

曾写过一篇关于 Oracle NULL 对 in 和 not in 结果的影响:Oracle 的 where 条件 in/not in 中包含 NULL 时的处理。今天来看看 exists 和 not exists 中 NULL 值对结果的影响。

网上经常看到关于 in 和 exixts、not in 和 not exists 性能比对和互换的例子,但它们真的就可以简单互换么?我们通过下面的实验来看一下。
实验环境:Oracle 11.2.0.4

1、创建表并插入测试数据

create table t1 (id number);
create table t2 (id number);
insert into t1 values(1);
insert into t1 values(2);
insert into t1 values(3);
insert into t1 values(4);
insert into t1 values(null);
commit;
insert into t2 values(3);
insert into t2 values(4);
insert into t2 values(5);
insert into t2 values(6);
commit;
linuxidc@ORA11G>select * from t1;
 
    ID
———-
    1
    2
    3
    4
 
 
5 rows selected. 
 
linuxidc@ORA11G>select * from t2;
 
    ID
———-
    3
    4
    5
    6
 
4 rows selected.

第一种情况:exists/in 的查询中不包含 NULL,外层查询包含 NULL
linuxidc@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
    3
    4
 
2 rows selected. 
 
linuxidc@ORA11G>select * from t1 where id in (select id from t2);
 
    ID
———-
    3
    4
 
2 rows selected.

从上面的查询结果看出 exists 和 in 都查到了 id= 2 和 3 的两条数据。
第二种情况:not exists/not in 的查询中不包含 NULL,外层查询包含 NULL
linuxidc@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
 
    1
    2
 
3 rows selected. 
 
linuxidc@ORA11G>select * from t1 where id not in (select id from t2);
 
    ID
———-
    1
    2
 
2 rows selected.

从上面的结果中可以看到两个查询都查到了 id= 1 和 2 这两条记录,但 not exists 还查到了 t1 表中 id 为 NULL 的行。原因是表 t1 中 id 为 NULL 的行 exists(3,4,5,6) 为 False,但前面加了个 not 则返回结果就为 True 了。
第三种情况:exists/in 的子查询中包含 NULL,外层查询包含 NULL
linuxidc@ORA11G>insert into t2 values(null);
 
1 row created. 
 
linuxidc@ORA11G>commit;
 
Commit complete. 
 
linuxidc@ORA11G>select * from t1 where id in (select id from t2);
 
    ID
———-
    3
    4
 
2 rows selected. 
 
linuxidc@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
    3
    4
 
2 rows selected.

从上面的结果中可以看出 exist 和 in 的结果是一致的。
第四种情况:not exists 和 not in 的查询中包含 NULL
linuxidc@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
 
    1
    2
 
3 rows selected. 
 
linuxidc@ORA11G>select * from t1 where id not in (select id from t2);
 
no rows selected

从上面的查询结果中可以看出两个结果差异很大,not exists 把 id= 1 和 2 和为 NULL 的值都查出来了,而 not in 查出来的结果为空。no in 结果为空的原因可以参考之前的文章,not exists 的原因与第二种情况类似。
第五种情况:not in/not exists 的子查询中无 NULL 值,外层查询也无 NULL 值
linuxidc@ORA11G>delete from t1 where id is null;
 
1 row deleted. 
 
linuxidc@ORA11G>delete from t2 where id is null;
 
1 row deleted. 
 
linuxidc@ORA11G>commit;
 
Commit complete. 
 
linuxidc@ORA11G>select * from t1 where id not in (select id from t2);
 
    ID
———-
    1
    2
 
2 rows selected. 
 
linuxidc@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
    1
    2
 
2 rows selected.

第六种情况:in/exists 的子查询中无 NULL 值,外层查询也无 NULL 值
linuxidc@ORA11G>select * from t1 where id in (select id from t2);
 
    ID
———-
    3
    4
 
2 rows selected. 
 
linuxidc@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
    3
    4
 
2 rows selected.

第七种情况:in/exists 的子查询中有 NULL 值,外层查询无 NULL 值
linuxidc@ORA11G>insert into t2 values(null);
 
1 row created. 
 
linuxidc@ORA11G>commit;
 
Commit complete. 
 
linuxidc@ORA11G>select * from t1 where id in (select id from t2);
 
    ID
———-
    3
    4
 
2 rows selected. 
 
linuxidc@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
    3
    4
 
2 rows selected.

第八种情况:not in/not exists 的子查询中有 NULL 值,外层查询无 NULL 值
linuxidc@ORA11G>select * from t1 where id not in (select id from t2);
 
no rows selected 
 
linuxidc@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);
 
    ID
———-
    1
    2
 
2 rows selected.

从上面的八种情况我们可以总结如下:
    1、in 和 exists 在有无 NULL 的情况下可以相互转换。

    2、not in 和 not exists 在都没有 NULL 值的情况下才可以相互转换。

更多 Oracle 相关信息见 Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-11/1487449.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计3370字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中