In ------ 遍曆
Exists ------- 檢索到滿足條件即退出
Not Exists --------檢索到不滿足條件即退出
本質區別:
Exists 由於Exist屬於外驅動,故會利用索引來檢索資料
In 則屬於內驅動 故不能利用索引檢索資料
其中,In和Not In類似全表掃描,效率低,一般用 Exist和NotExist代替其用法。
使用環境:
*Exists 使用外串連查詢時用到。
*In 使用內串連查詢時用到。
e.g.:
In
的用法:
1 |
Select Top
10 ExpoName,ExpoClassID |
3 |
Where ExpoClassID
in (Select
Classid From tb_Expo_Class Where ParentID=0) |
其中,先執行 Select Classid From tb_Expo_Class Where ParentID=0
等價於:
1 |
Select Top
10 a.ExpoName From tb_Expo a, |
2 |
(select
Classid from tb_expo_class where parentid=0) b |
3 |
Where a.ExpoClassID= b.ClassID |
而Exist不同:
1 |
select top
10 ExpoName,ExpoClassID from
tb_Expo e |
2 |
where Exists (select
0 from tb_expo_class where parentid=0) |
其執行類似於下面的sql:
05 |
for
tb_Expo in (Select ExpoName,ExpoClassID
From tb_Expo) loop |
06 |
Select
count(*) into
l_count From tb_Expo_Class |
10 |
dbms_output.put_line(e.ExpoName); |
在查詢資料量大的時候就會體現出效率來。
當然,也不能說Exist就比In好。
如果
Select 0 from tb_expo_class where parentid=0
查詢出來的資料量很少的話,還是 In 效率更高些。