IN&EXISTS與NOT IN&NOT EXISTS 的優(yōu)化原則小結(jié)
更新時間:2010年06月21日 11:57:50 作者:
下面只是從理論上提出了一些建議,最好的原則是大家在上面的基礎(chǔ)上,能夠使用執(zhí)行計劃來分析,得出最佳的語句的寫法。
1. EXISTS的執(zhí)行流程
select * from t1 where exists ( select null from t2 where y = x )
可以理解為:
for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop
對于in 和 exists的性能區(qū)別:
如果子查詢得出的結(jié)果集記錄較少,主查詢中的表較大且又有索引時應(yīng)該用in,反之如果外層的主查詢記錄較少,子查詢中的表大,又有索引時使用exists。
其實我們區(qū)分in和exists主要是造成了驅(qū)動順序的改變(這是性能變化的關(guān)鍵),如果是exists,那么以外層表為驅(qū)動表,先被訪問,如果是IN,那么先執(zhí)行子查詢,所以我們會以驅(qū)動表的快速返回為目標(biāo),那么就會考慮到索引及結(jié)果集的關(guān)系了
另外IN時不對NULL進(jìn)行處理,如:
select 1 from dual where null in (0,1,2,null)
結(jié)果為空。
2. NOT IN 與NOT EXISTS:
NOT EXISTS的執(zhí)行流程
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
可以理解為:
for x in ( select * from rollup )
loop
if ( not exists ( that query ) ) then
OUTPUT
end if;
end;
注意:NOT EXISTS 與 NOT IN 不能完全互相替換,看具體的需求。如果選擇的列可以為空,則不能被替換。
例如下面語句,看他們的區(qū)別:
select x,y from t;
x y
------ ------
1 3
3 1
1 2
1 1
3 1
5
select * from t where x not in (select y from t t2 )
no rows
select * from t where not exists (select null from t t2
where t2.y=t.x )
x y
------ ------
5 NULL
所以要具體需求來決定
對于not in 和 not exists的性能區(qū)別:
not in 只有當(dāng)子查詢中,select 關(guān)鍵字后的字段有not null約束或者有這種暗示時用not in,另外如果主查詢中表大,子查詢中的表小但是記錄多,則應(yīng)當(dāng)使用not in,并使用anti hash join.
如果主查詢表中記錄少,子查詢表中記錄多,并有索引,可以使用not exists,另外not in最好也可以用/*+ HASH_AJ */或者外連接+is null
NOT IN 在基于成本的應(yīng)用中較好
比如:
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
改成(佳)
select ......
from title T, rollup R
where R.source_id = T.Title_id(+)
and T.Title_id is null;
或者(佳)
sql> select /*+ HASH_AJ */ ...
from rollup R
where ource_id NOT IN ( select ource_id
from title T
where ource_id IS NOT NULL )
注意:上面只是從理論上提出了一些建議,最好的原則是大家在上面的基礎(chǔ)上,能夠使用執(zhí)行計劃來分析,得出最佳的語句的寫法。
'//=============================
exists,not exists總結(jié)
1 exists
SELECT * FROM anken_m WHERE EXISTS(
SELECT my_list_temp_m.sales_code
FROM my_list_temp_m
WHERE my_list_temp_m.sales_code=anken_m.sales_code)
說明:
1) 查詢在anken_m表和my_list_temp_m表中都存在的sales_code。
2) sales_code是anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) 外層查詢表anken_m是查詢的對象。
2) 內(nèi)層查詢表my_list_temp_m是條件對象。
3) 內(nèi)外層的查詢表不能相同。
4) 作為關(guān)聯(lián)條件的anken_m表不需要在內(nèi)層查詢FROM后添加。
5) my_list_temp_m.sales_code=anken_m.sales_code條件的左右順序不影響查詢結(jié)果。
2 not exists
SELECT * FROM anken_m WHERE NOT EXISTS(
SELECT my_list_temp_m.sales_code
FROM my_list_temp_m
WHERE my_list_temp_m.sales_code=anken_m.sales_code)
說明:
1) 查詢在anken_m表中存在,但是在my_list_temp_m表中不存在的sales_code。
2) sales_code是anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) 外層查詢表anken_m是查詢的對象。
2) 內(nèi)層查詢表my_list_temp_m是條件對象。
3) 內(nèi)外層的查詢表不能相同。
4) 作為關(guān)聯(lián)條件的anken_m表不需要在內(nèi)層查詢FROM后添加。
5) my_list_temp_m.sales_code=anken_m.sales_code條件的左右順序不影響查詢結(jié)果。
3 綜合運用
UPDATE anken_m
SET(plan_type_code, branch_name, business_type_code)
=(SELECT anken.plan_type_code,anken.branch_name,anken.business_type_code
FROM anken
WHERE anken.sales_code=anken_m.sales_code)
WHERE EXISTS (
SELECT anken.sales_code
FROM anken,my_list_temp_m
WHERE my_list_temp_m.sales_code=anken.sales_code
AND anken.sales_code=anken_m.sales_code
)
說明:
1) 用一個表的記錄數(shù)據(jù)更新另一個表的記錄數(shù)據(jù)。
2) 用一個SQL語句進(jìn)行批量更新。
2) sales_code是anken,anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) set 語句中的要被更新字段必須跟數(shù)據(jù)源字段一一對應(yīng),另外數(shù)據(jù)源查詢中的條件必須限定一條記錄。也就是根據(jù)sales_code可以唯一確定anken的一條記錄,和anken_m的一條記錄,這樣才能保證要被更新的記錄和數(shù)據(jù)源記錄的主鍵是相同的。
2) 根據(jù)WHERE EXISTS語句可以確定數(shù)據(jù)源記錄的范圍,也就是可以用anken表中哪些記錄更新anken_m表。所以anken_m不需要在WHERE EXISTS語句中的FROM后添加。
select * from t1 where exists ( select null from t2 where y = x )
可以理解為:
復(fù)制代碼 代碼如下:
for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop
對于in 和 exists的性能區(qū)別:
如果子查詢得出的結(jié)果集記錄較少,主查詢中的表較大且又有索引時應(yīng)該用in,反之如果外層的主查詢記錄較少,子查詢中的表大,又有索引時使用exists。
其實我們區(qū)分in和exists主要是造成了驅(qū)動順序的改變(這是性能變化的關(guān)鍵),如果是exists,那么以外層表為驅(qū)動表,先被訪問,如果是IN,那么先執(zhí)行子查詢,所以我們會以驅(qū)動表的快速返回為目標(biāo),那么就會考慮到索引及結(jié)果集的關(guān)系了
另外IN時不對NULL進(jìn)行處理,如:
select 1 from dual where null in (0,1,2,null)
結(jié)果為空。
2. NOT IN 與NOT EXISTS:
NOT EXISTS的執(zhí)行流程
復(fù)制代碼 代碼如下:
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
可以理解為:
復(fù)制代碼 代碼如下:
for x in ( select * from rollup )
loop
if ( not exists ( that query ) ) then
OUTPUT
end if;
end;
注意:NOT EXISTS 與 NOT IN 不能完全互相替換,看具體的需求。如果選擇的列可以為空,則不能被替換。
例如下面語句,看他們的區(qū)別:
select x,y from t;
x y
------ ------
1 3
3 1
1 2
1 1
3 1
5
select * from t where x not in (select y from t t2 )
no rows
select * from t where not exists (select null from t t2
where t2.y=t.x )
x y
------ ------
5 NULL
所以要具體需求來決定
對于not in 和 not exists的性能區(qū)別:
not in 只有當(dāng)子查詢中,select 關(guān)鍵字后的字段有not null約束或者有這種暗示時用not in,另外如果主查詢中表大,子查詢中的表小但是記錄多,則應(yīng)當(dāng)使用not in,并使用anti hash join.
如果主查詢表中記錄少,子查詢表中記錄多,并有索引,可以使用not exists,另外not in最好也可以用/*+ HASH_AJ */或者外連接+is null
NOT IN 在基于成本的應(yīng)用中較好
比如:
復(fù)制代碼 代碼如下:
select .....
from rollup R
where not exists ( select 'Found' from title T
where R.source_id = T.Title_ID);
改成(佳)
select ......
from title T, rollup R
where R.source_id = T.Title_id(+)
and T.Title_id is null;
或者(佳)
sql> select /*+ HASH_AJ */ ...
from rollup R
where ource_id NOT IN ( select ource_id
from title T
where ource_id IS NOT NULL )
注意:上面只是從理論上提出了一些建議,最好的原則是大家在上面的基礎(chǔ)上,能夠使用執(zhí)行計劃來分析,得出最佳的語句的寫法。
'//=============================
exists,not exists總結(jié)
1 exists
SELECT * FROM anken_m WHERE EXISTS(
SELECT my_list_temp_m.sales_code
FROM my_list_temp_m
WHERE my_list_temp_m.sales_code=anken_m.sales_code)
說明:
1) 查詢在anken_m表和my_list_temp_m表中都存在的sales_code。
2) sales_code是anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) 外層查詢表anken_m是查詢的對象。
2) 內(nèi)層查詢表my_list_temp_m是條件對象。
3) 內(nèi)外層的查詢表不能相同。
4) 作為關(guān)聯(lián)條件的anken_m表不需要在內(nèi)層查詢FROM后添加。
5) my_list_temp_m.sales_code=anken_m.sales_code條件的左右順序不影響查詢結(jié)果。
2 not exists
SELECT * FROM anken_m WHERE NOT EXISTS(
SELECT my_list_temp_m.sales_code
FROM my_list_temp_m
WHERE my_list_temp_m.sales_code=anken_m.sales_code)
說明:
1) 查詢在anken_m表中存在,但是在my_list_temp_m表中不存在的sales_code。
2) sales_code是anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) 外層查詢表anken_m是查詢的對象。
2) 內(nèi)層查詢表my_list_temp_m是條件對象。
3) 內(nèi)外層的查詢表不能相同。
4) 作為關(guān)聯(lián)條件的anken_m表不需要在內(nèi)層查詢FROM后添加。
5) my_list_temp_m.sales_code=anken_m.sales_code條件的左右順序不影響查詢結(jié)果。
3 綜合運用
UPDATE anken_m
SET(plan_type_code, branch_name, business_type_code)
=(SELECT anken.plan_type_code,anken.branch_name,anken.business_type_code
FROM anken
WHERE anken.sales_code=anken_m.sales_code)
WHERE EXISTS (
SELECT anken.sales_code
FROM anken,my_list_temp_m
WHERE my_list_temp_m.sales_code=anken.sales_code
AND anken.sales_code=anken_m.sales_code
)
說明:
1) 用一個表的記錄數(shù)據(jù)更新另一個表的記錄數(shù)據(jù)。
2) 用一個SQL語句進(jìn)行批量更新。
2) sales_code是anken,anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) set 語句中的要被更新字段必須跟數(shù)據(jù)源字段一一對應(yīng),另外數(shù)據(jù)源查詢中的條件必須限定一條記錄。也就是根據(jù)sales_code可以唯一確定anken的一條記錄,和anken_m的一條記錄,這樣才能保證要被更新的記錄和數(shù)據(jù)源記錄的主鍵是相同的。
2) 根據(jù)WHERE EXISTS語句可以確定數(shù)據(jù)源記錄的范圍,也就是可以用anken表中哪些記錄更新anken_m表。所以anken_m不需要在WHERE EXISTS語句中的FROM后添加。
相關(guān)文章
sql server Bulk Insert命令詳細(xì)
這篇文章主要介紹了sql server Bulk Insert命令詳細(xì) ,需要的朋友可以參考下2015-08-08卸載VS2011 Developer Preview后Sql Server2008 R2建立數(shù)據(jù)庫關(guān)系圖報“找不到指定的
話說上回我為了嘗嘗螃蟹的味道而裝了 VS2011 Developer Preview,但是裝完后立馬卸載掉了,原因是這家伙的安裝目錄位置沒用,我設(shè)置到D盤的但是裝完后D盤的文件夾只有一百多M,而足足8G+的空間是在C盤上消耗的。2011-11-11SQL SERVER 2012數(shù)據(jù)庫自動備份的方法
這篇文章主要為大家詳細(xì)介紹了SQL SERVER 2012數(shù)據(jù)庫自動備份的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10SQL Server 提取數(shù)字、提取英文、提取中文的sql語句
這篇文章主要介紹了SQL Server 提取數(shù)字、提取英文、提取中文 ,需要的朋友可以參考下2014-10-10