在存在in的子查詢的SQL語句和存在EXISTS的相關子查詢的SQL語句的執行計劃里,有NESTED LOOPS SEMI (即半嵌套迴圈)。 所謂的NESTED LOOPS SEMI (即半嵌套迴圈),就是 the out query stops evaluating (評價,求…的數值)the ...
在存在in的子查詢的SQL語句和存在EXISTS的相關子查詢的SQL語句的執行計劃里,有NESTED LOOPS SEMI (即半嵌套迴圈)。
所謂的NESTED LOOPS SEMI (即半嵌套迴圈),就是
the out query stops evaluating (評價,求…的數值)the result set of the inner query when the first value is found。
也就是說,一旦子查詢的第一條結果出來,主查詢(里的表的當前行)就停止子查詢的繼續進行執行。
NESTED LOOPS SEMI (即半嵌套迴圈)
open tab1 (主查詢里的表) while tab1 still has records fetch one record from tab1 (並且) result = false (即將變數result的值置為alse) open tab2 while tab2 still has records fetch one record from tab2 if(根據tab1.record 和 tab2.record的值執行一次子查詢語句所得的結果集不為空) then result = true (並且)exit loop2 end if end loop2 close tab2 if (result = true) return tab1 record end loop1 close tab1
註釋:
fetch one record from tab1 result = false (即將變數result的值置為alse)open tab2這三條語句是併列的關係
result = true exit loop2 這兩條語句是併列的關係
在存在in的SQL語句的執行計劃里的NESTED LOOPS SEMI (即半嵌套迴圈):
gyj@MYDB> set autot traceonly; gyj@MYDB> select * from t4 where id in (select id from t3); 9 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1092212754 ----------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 9 | 99 | 21 (0)| 00:00:01 | | 1 | NESTED LOOPS SEMI | | 9 | 99 | 21 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL| T4 | 9 | 54 | 3 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | IDX_T3 | 999K| 4882K| 2 (0)| 00:00:01 | ----------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("ID"="ID") Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 20 consistent gets 0 physical reads 0 redo size 723 bytes sent via SQL*Net to client 520 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 9 rows processed
存在EXISTS的相關子查詢的SQL語句的執行計劃里的NESTED LOOPS SEMI (即半嵌套迴圈):
open tab1 while tab1 still has records fetch record from tab1 result = false open tab2 while tab2 still has records fetch record from tab2 if(tab1.record matches tab2.record) then result = true exit loop end if end loop close tab2 if (result = true) return tab1 record end loop close tab1
註釋:
1#
EXISTS謂詞非常簡單,它是對一個非空集的測試。如果在其子查詢中存在任何行,則返回TRUE,否則為FALSE。該謂詞不會返回UNKNOWN結果。EXIST()謂詞語法如下: <EXISTS謂詞>::=[NOTEXISTS]<表子查詢>
2#
執行計劃中,若一個父操作有兩條併列的子操作時,其執行模式之一是:
第一條子操作都是先執行,其影響下一條併列的子操作執行,也就是說第一條子操作遍歷一遍表A後父操作才算結束,當該子操作遍歷一行表A上的數據時,另一個子操作就會遍歷一遍表B。例如,
| 1 | NESTED LOOPS SEMI | | 9 | 99 | 21 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL| T4 | 9 | 54 | 3 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | IDX_T3 | 999K| 4882K| 2 (0)| 00:00:01 |
參見:http://blog.csdn.net/haiross/article/details/42143853