程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> IN&EXISTS與NOT IN&NOT EXISTS 的優化原則小結

IN&EXISTS與NOT IN&NOT EXISTS 的優化原則小結

編輯:更多數據庫知識

1. EXISTS的執行流程
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的性能區別:
如果子查詢得出的結果集記錄較少,主查詢中的表較大且又有索引時應該用in,反之如果外層的主查詢記錄較少,子查詢中的表大,又有索引時使用exists。
其實我們區分in和exists主要是造成了驅動順序的改變(這是性能變化的關鍵),如果是exists,那麼以外層表為驅動表,先被訪問,如果是IN,那麼先執行子查詢,所以我們會以驅動表的快速返回為目標,那麼就會考慮到索引及結果集的關系了
另外IN時不對NULL進行處理,如:
select 1 from dual where null in (0,1,2,null)
結果為空。

2. NOT IN 與NOT EXISTS:
NOT EXISTS的執行流程
復制代碼 代碼如下:
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 不能完全互相替換,看具體的需求。如果選擇的列可以為空,則不能被替換。
例如下面語句,看他們的區別:
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的性能區別:
not in 只有當子查詢中,select 關鍵字後的字段有not null約束或者有這種暗示時用not in,另外如果主查詢中表大,子查詢中的表小但是記錄多,則應當使用not in,並使用anti hash join.
如果主查詢表中記錄少,子查詢表中記錄多,並有索引,可以使用not exists,另外not in最好也可以用/*+ HASH_AJ */或者外連接+is null
NOT IN 在基於成本的應用中較好
比如:
復制代碼 代碼如下:
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 )
注意:上面只是從理論上提出了一些建議,最好的原則是大家在上面的基礎上,能夠使用執行計劃來分析,得出最佳的語句的寫法。
'//=============================
exists,not exists總結

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) 內層查詢表my_list_temp_m是條件對象。
3) 內外層的查詢表不能相同。
4) 作為關聯條件的anken_m表不需要在內層查詢FROM後添加。
5) my_list_temp_m.sales_code=anken_m.sales_code條件的左右順序不影響查詢結果。

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) 內層查詢表my_list_temp_m是條件對象。
3) 內外層的查詢表不能相同。
4) 作為關聯條件的anken_m表不需要在內層查詢FROM後添加。
5) my_list_temp_m.sales_code=anken_m.sales_code條件的左右順序不影響查詢結果。

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) 用一個表的記錄數據更新另一個表的記錄數據。
2) 用一個SQL語句進行批量更新。
2) sales_code是anken,anken_m的主鍵,my_list_temp_m的外鍵。
注意:
1) set 語句中的要被更新字段必須跟數據源字段一一對應,另外數據源查詢中的條件必須限定一條記錄。也就是根據sales_code可以唯一確定anken的一條記錄,和anken_m的一條記錄,這樣才能保證要被更新的記錄和數據源記錄的主鍵是相同的。
2) 根據WHERE EXISTS語句可以確定數據源記錄的范圍,也就是可以用anken表中哪些記錄更新anken_m表。所以anken_m不需要在WHERE EXISTS語句中的FROM後添加。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved