之前写过一篇关于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、创建表并插入测试数据
createtablet1(idnumber); createtablet2(idnumber); insertintot1values(1); insertintot1values(2); insertintot1values(3); insertintot1values(4); insertintot1values(null); commit; insertintot2values(3); insertintot2values(4); insertintot2values(5); insertintot2values(6); commit; zx@ORA11G>select*fromt1; ID ---------- 1 2 3 4 5rowsselected. zx@ORA11G>select*fromt2; ID ---------- 3 4 5 6 4rowsselected.
第一种情况:exists/in的查询中不包含NULL,外层查询包含NULL
zx@ORA11G>select*fromt1whereexists(select1fromt2wheret1.id=t2.id); ID ---------- 3 4 2rowsselected. zx@ORA11G>select*fromt1whereidin(selectidfromt2); ID ---------- 3 4 2rowsselected.
从上面的查询结果看出exists和in都查到了id=2和3的两条数据。
第二种情况:not exists/not in的查询中不包含NULL,外层查询包含NULL
zx@ORA11G>select*fromt1wherenotexists(select1fromt2wheret1.id=t2.id); ID ---------- 1 2 3rowsselected. zx@ORA11G>select*fromt1whereidnotin(selectidfromt2); ID ---------- 1 2 2rowsselected.
从上面的结果中可以看到两个查询都查到了id=1和2这两条记录,但not exists还查到了t1表中id为NULL的行。原因是表t1中id为NULL的行exists(3,4,5,6)为False,但前面加了个not则返回结果就为True了。
第三种情况:exists/in的子查询中包含NULL,外层查询包含NULL
zx@ORA11G>insertintot2values(null); 1rowcreated. zx@ORA11G>commit; Commitcomplete. zx@ORA11G>select*fromt1whereidin(selectidfromt2); ID ---------- 3 4 2rowsselected. zx@ORA11G>select*fromt1whereexists(select1fromt2wheret1.id=t2.id); ID ---------- 3 4 2rowsselected.
从上面的结果中可以看出exist和in的结果是一致的。
第四种情况:not exists和not in的查询中包含NULL
zx@ORA11G>select*fromt1wherenotexists(select1fromt2wheret1.id=t2.id); ID ---------- 1 2 3rowsselected. zx@ORA11G>select*fromt1whereidnotin(selectidfromt2); norowsselected
从上面的查询结果中可以看出两个结果差异很大,not exists把id=1和2和为NULL的值都查出来了,而not in查出来的结果为空。no in结果为空的原因可以参考之前的文章,not exists的原因与第二种情况类似。
第五种情况:not in/not exists的子查询中无NULL值,外层查询也无NULL值
zx@ORA11G>deletefromt1whereidisnull; 1rowdeleted. zx@ORA11G>deletefromt2whereidisnull; 1rowdeleted. zx@ORA11G>commit; Commitcomplete. zx@ORA11G>select*fromt1whereidnotin(selectidfromt2); ID ---------- 1 2 2rowsselected. zx@ORA11G>select*fromt1wherenotexists(select1fromt2wheret1.id=t2.id); ID ---------- 1 2 2rowsselected.
第六种情况:in/exists的子查询中无NULL值,外层查询也无NULL值
zx@ORA11G>select*fromt1whereidin(selectidfromt2); ID ---------- 3 4 2rowsselected. zx@ORA11G>select*fromt1whereexists(select1fromt2wheret1.id=t2.id); ID ---------- 3 4 2rowsselected.
第七种情况:in/exists的子查询中有NULL值,外层查询无NULL值
zx@ORA11G>insertintot2values(null); 1rowcreated. zx@ORA11G>commit; Commitcomplete. zx@ORA11G>select*fromt1whereidin(selectidfromt2); ID ---------- 3 4 2rowsselected. zx@ORA11G>select*fromt1whereexists(select1fromt2wheret1.id=t2.id); ID ---------- 3 4 2rowsselected.
第八种情况:not in/not exists的子查询中有NULL值,外层查询无NULL值
zx@ORA11G>select*fromt1whereidnotin(selectidfromt2); norowsselected zx@ORA11G>select*fromt1wherenotexists(select1fromt2wheret1.id=t2.id); ID ---------- 1 2 2rowsselected.
从上面的八种情况我们可以总结如下:
1、in和exists在有无NULL的情况下可以相互转换。
2、not in和not exists在都没有NULL值的情况下才可以相互转换。
参考:https://mp.weixin.qq.com/s/rHKBFMQrrBf1TiUo6UmEmQ
http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions013.htm#SQLRF52169
http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions012.htm#SQLRF52167
原文链接:https://www.f2er.com/oracle/207894.html