之前寫(xiě)過(guò)一篇關(guān)于NULL對(duì)in和not in結(jié)果的影響:Oracle的where條件in/not in中包含NULL時(shí)的處理。今天來(lái)看看exists和not exists中NULL值對(duì)結(jié)果的影響。
目前創(chuàng)新互聯(lián)建站已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、洪山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
網(wǎng)上經(jīng)常看到關(guān)于in和exixts、not in和not exists性能比對(duì)和互換的例子,但它們真的就可以簡(jiǎn)單互換么?我們通過(guò)下面的實(shí)驗(yàn)來(lái)看一下。
實(shí)驗(yàn)環(huán)境:Oracle 11.2.0.4
1、創(chuàng)建表并插入測(cè)試數(shù)據(jù)
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; zx@ORA11G>select * from t1; ID ---------- 1 2 3 4 5 rows selected. zx@ORA11G>select * from t2; ID ---------- 3 4 5 6 4 rows selected.
第一種情況:exists/in的查詢(xún)中不包含NULL,外層查詢(xún)包含NULL
zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected.
從上面的查詢(xún)結(jié)果看出exists和in都查到了id=2和3的兩條數(shù)據(jù)。
第二種情況:not exists/not in的查詢(xún)中不包含NULL,外層查詢(xún)包含NULL
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 3 rows selected. zx@ORA11G>select * from t1 where id not in (select id from t2); ID ---------- 1 2 2 rows selected.
從上面的結(jié)果中可以看到兩個(gè)查詢(xún)都查到了id=1和2這兩條記錄,但not exists還查到了t1表中id為NULL的行。原因是表t1中id為NULL的行exists(3,4,5,6)為False,但前面加了個(gè)not則返回結(jié)果就為T(mén)rue了。
第三種情況:exists/in的子查詢(xún)中包含NULL,外層查詢(xún)包含NULL
zx@ORA11G>insert into t2 values(null); 1 row created. zx@ORA11G>commit; Commit complete. zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected.
從上面的結(jié)果中可以看出exist和in的結(jié)果是一致的。
第四種情況:not exists和not in的查詢(xún)中包含NULL
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 3 rows selected. zx@ORA11G>select * from t1 where id not in (select id from t2); no rows selected
從上面的查詢(xún)結(jié)果中可以看出兩個(gè)結(jié)果差異很大,not exists把id=1和2和為NULL的值都查出來(lái)了,而not in查出來(lái)的結(jié)果為空。no in結(jié)果為空的原因可以參考之前的文章,not exists的原因與第二種情況類(lèi)似。
第五種情況:not in/not exists的子查詢(xún)中無(wú)NULL值,外層查詢(xún)也無(wú)NULL值
zx@ORA11G>delete from t1 where id is null; 1 row deleted. zx@ORA11G>delete from t2 where id is null; 1 row deleted. zx@ORA11G>commit; Commit complete. zx@ORA11G>select * from t1 where id not in (select id from t2); ID ---------- 1 2 2 rows selected. zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 2 rows selected.
第六種情況:in/exists的子查詢(xún)中無(wú)NULL值,外層查詢(xún)也無(wú)NULL值
zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected. zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id); ID ---------- 3 4 2 rows selected.
第七種情況:in/exists的子查詢(xún)中有NULL值,外層查詢(xún)無(wú)NULL值
zx@ORA11G>insert into t2 values(null); 1 row created. zx@ORA11G>commit; Commit complete. zx@ORA11G>select * from t1 where id in (select id from t2); ID ---------- 3 4 2 rows selected. zx@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的子查詢(xún)中有NULL值,外層查詢(xún)無(wú)NULL值
zx@ORA11G>select * from t1 where id not in (select id from t2); no rows selected zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id); ID ---------- 1 2 2 rows selected.
從上面的八種情況我們可以總結(jié)如下:
1、in和exists在有無(wú)NULL的情況下可以相互轉(zhuǎn)換。
2、not in和not exists在都沒(méi)有NULL值的情況下才可以相互轉(zhuǎn)換。
參考: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