oracle执行计划中NESTED LOOPS SEMI (即半嵌套循环)的解释

来源:互联网 发布:opencv threshold源码 编辑:程序博客网 时间:2024/05/15 02:29

在存在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 (即半嵌套循环)执行过程的伪代码如下:

  1. open tab1  (主查询里的表
  2.  while tab1 still has records  
  3.     fetch one record from tab1  
  4.    (并且) result = false  (即将变量result的值置为alse
  5.     open tab2  
  6.     while tab2 still has records  
  7.         fetch one record from tab2  
  8.         if(根据tab1.record 和 tab2.record的值执行一次子查询语句所得的结果集不为空) then  
  9.            result = true  
  10.            (并且)exit loop2 
  11.          end if  
  12.     end loop2  
  13.     close tab2  
  14.     if (result = true) return tab1 record  
  15.  end loop1  
  16.  close tab1 
注释:

  • fetch one record from tab1  
  • result = false  (即将变量result的值置为alse
  • open tab2
  • 这三条语句是并列的关系  


  • result = true  
  • exit loop2 
  • 这两条语句是并列的关系 


    在存在in的SQL语句的执行计划里的NESTED LOOPS SEMI (即半嵌套循环):


    [html] view plaincopy
    1. gyj@MYDB> set autot traceonly;  
    2. gyj@MYDB> select * from t4 where id in (select id from t3);  
    3.   
    4. 9 rows selected.  
    5.   
    6.   
    7. Execution Plan  
    8. ----------------------------------------------------------  
    9. Plan hash value: 1092212754  
    10.   
    11. -----------------------------------------------------------------------------  
    12. | Id  | Operation          | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
    13. -----------------------------------------------------------------------------  
    14. |   0 | SELECT STATEMENT   |        |     9 |    99 |    21   (0)| 00:00:01 |  
    15. |   1 |  NESTED LOOPS SEMI |        |     9 |    99 |    21   (0)| 00:00:01 |  
    16. |   2 |   TABLE ACCESS FULL| T4     |     9 |    54 |     3   (0)| 00:00:01 |  
    17. |*  3 |   INDEX RANGE SCAN | IDX_T3 |   999K|  4882K|     2   (0)| 00:00:01 |  
    18. -----------------------------------------------------------------------------  
    19.   
    20. Predicate Information (identified by operation id):  
    21. ---------------------------------------------------  
    22.   
    23.    3 - access("ID"="ID")  
    24.   
    25.   
    26. Statistics  
    27. ----------------------------------------------------------  
    28.           1  recursive calls  
    29.           0  db block gets  
    30.          20  consistent gets  
    31.           0  physical reads  
    32.           0  redo size  
    33.         723  bytes sent via SQL*Net to client  
    34.         520  bytes received via SQL*Net from client  
    35.           2  SQL*Net roundtrips to/from client  
    36.           0  sorts (memory)  
    37.           0  sorts (disk)  
    38.           9  rows processed  


    存在EXISTS的相关子查询的SQL语句的执行计划里的NESTED LOOPS SEMI (即半嵌套循环)


    [html] view plaincopy
    1. open tab1  
    2.  while tab1 still has records  
    3.     fetch  record from tab1  
    4.     result = false  
    5.     open tab2  
    6.     while tab2 still has records  
    7.         fetch record from tab2  
    8.         if(tab1.record matches tab2.record) then  
    9.            result = true  
    10.            exit loop  
    11.          end if  
    12.     end loop  
    13.     close tab2  
    14.     if (result = true) return tab1 record  
    15.  end loop  
    16.  close tab1  

    注释:

    1#

  • EXISTS谓词非常简单,它是对一个非空集的测试。如果在其子查询中存在任何行,则返回TRUE,否则为FALSE。该谓词不会返回UNKNOWN结果。EXIST()谓词语法如下: <EXISTS谓词>::=[NOTEXISTS]<表子查询>

    2#

  • 执行计划中,若一个父操作有两条并列的子操作时,其执行模式之一是:

    第一条子操作都是先执行,其影响下一条并列的子操作执行,也就是说第一条子操作遍历一遍表A后父操作才算结束,当该子操作遍历一行表A上的数据时,另一个子操作就会遍历一遍表B。例如,

    1. |    1 |  NESTED LOOPS SEMI |                |     9      |    99 |    21   (0)| 00:00:01 |  
    2. |    2 |   TABLE ACCESS FULL| T4          |     9      |    54 |     3   (0)| 00:00:01 |  
    3. |*  3 |   INDEX RANGE SCAN   | IDX_T3 |   999K|  4882K|     2   (0)| 00:00:01 |  
  • 0 0