在存在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