Oracle执行计划 讲解(一)

来源:互联网 发布:网络模块接法图解 编辑:程序博客网 时间:2024/05/20 22:26

看懂Oracle执行计划是优化的第一步,让我们从下面的例子开始吧。

        下面为补充内容

1、创建测试表

[sql] view plain copy
  1. SQL> create table t as select 1 id,object_name from dba_objects;  
  2.    
  3. Table created  
  4.    
  5. SQL> update t set id=99 where rownum=1;  
  6.    
  7. 1 row updated  
  8.    
  9. SQL> commit;  
  10.    
  11. Commit complete  
  12.    
  13. SQL> create index t_ind on t(id);  
  14.    
  15. Index created  

        oracle优化器:RBO和CBO两种, 从oracle10g开始优化器已经抛弃了RBO,下面的列子说明CBO大概是怎样的

[sql] view plain copy
  1. SQL>  select /*+dynamic_sampling(t 0) */* from t where id=1;  
  2.   
  3. 50819 rows selected.  
  4.   
  5.   
  6. Execution Plan  
  7. ----------------------------------------------------------  
  8. Plan hash value: 1376202287  
  9.   
  10. -------------------------------------------------------------------------------------  
  11. | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  12. -------------------------------------------------------------------------------------  
  13. |   0 | SELECT STATEMENT            |       |   195 | 15405 |    51   (0)| 00:00:01 |  
  14. |   1 |  TABLE ACCESS BY INDEX ROWID| T     |   195 | 15405 |    51   (0)| 00:00:01 |  
  15. |*  2 |   INDEX RANGE SCAN          | T_IND |    78 |       |    50   (0)| 00:00:01 |  
  16. -------------------------------------------------------------------------------------  
  17.   
  18. Predicate Information (identified by operation id):  
  19. ---------------------------------------------------  
  20.   
  21.    2 - access("ID"=1)  

       现象t表还没有被分析,提示/*+dynamic_sampling(t 0) */*的目的是让CBO无法通过动态采样获取表中的实际数据情况,此时CBO只能根据T表中非常有限的信息(比如表中的extents数量,数据块的数量)来猜测表中的数据。从结果中可以看到CBO猜出表中id=1的有195条,这个数值对于表的总数来说,是一个非常小的值,所以CBO选择了索引而不是全表扫描。
      而实际情况如下所示:
[sql] view plain copy
  1. SQL> select * from  t where id=1  
  2.   2  ;  
  3.   
  4. 50819 rows selected.  
  5.   
  6.   
  7. Execution Plan  
  8. ----------------------------------------------------------  
  9. Plan hash value: 1601196873  
  10.   
  11. --------------------------------------------------------------------------  
  12. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  13. --------------------------------------------------------------------------  
  14. |   0 | SELECT STATEMENT  |      | 49454 |  3815K|    67   (2)| 00:00:01 |  
  15. |*  1 |  TABLE ACCESS FULL| T    | 49454 |  3815K|    67   (2)| 00:00:01 |  
  16. --------------------------------------------------------------------------  
  17.   
  18. Predicate Information (identified by operation id):  
  19. ---------------------------------------------------  
  20.   
  21.    1 - filter("ID"=1)  

       通过动态取样,CBO估算出行数为49454,非常接近于真实50820数目。选择了全表扫描。
       我们来收集一下统计信息

[sql] view plain copy
  1. SQL> exec dbms_stats.gather_table_stats(user,'t',cascade => true);  
  2.   
  3. SQL> select * from  t where id=1;  
  4.   
  5. 50819 rows selected.  
  6.   
  7.   
  8. Execution Plan  
  9. ----------------------------------------------------------  
  10. Plan hash value: 1601196873  
  11.   
  12. --------------------------------------------------------------------------  
  13. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  14. --------------------------------------------------------------------------  
  15. |   0 | SELECT STATEMENT  |      | 50815 |  1339K|    67   (2)| 00:00:01 |  
  16. |*  1 |  TABLE ACCESS FULL| T    | 50815 |  1339K|    67   (2)| 00:00:01 |  
  17. --------------------------------------------------------------------------  
  18.   
  19. Predicate Information (identified by operation id):  
  20. ---------------------------------------------------  
  21.   
  22.    1 - filter("ID"=1)  

现在扫描过的行数为50815。

如果我们更新了所有的id为99看看。

[sql] view plain copy
  1. SQL> update t set id=99;  
  2.    
  3. 50820 rows updated  
  4.   
  5. SQL> select * from  t where id=99;  
  6.   
  7.   
  8. Execution Plan  
  9. ----------------------------------------------------------  
  10. Plan hash value: 1376202287  
  11.   
  12. -------------------------------------------------------------------------------------  
  13. | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  14. -------------------------------------------------------------------------------------  
  15. |   0 | SELECT STATEMENT            |       |     1 |    27 |     2   (0)| 00:00:01 |  
  16. |   1 |  TABLE ACCESS BY INDEX ROWID| T     |     1 |    27 |     2   (0)| 00:00:01 |  
  17. |*  2 |   INDEX RANGE SCAN          | T_IND |     1 |       |     1   (0)| 00:00:01 |  
  18. -------------------------------------------------------------------------------------  
  19.   
  20. Predicate Information (identified by operation id):  
  21. ---------------------------------------------------  
  22.   
  23.    2 - access("ID"=99)  

        因为没有对表进行分析,所以表中的分析数据还是之前的信息,CBO并不知道。我们可以看出Rows值为1,也就是说CBO人为表T中的ID=99的值只有1条,所有选择仍然是索引。
 
       我们收集一把统计信息。 

[sql] view plain copy
  1. SQL> exec dbms_stats.gather_table_stats(user,'t',cascade => true);  
  2.    
  3. PL/SQL procedure successfully completed  
  4.   
  5. SQL> select * from  t where id=99;  
  6.   
  7. 50820 rows selected.  
  8.   
  9.   
  10. Execution Plan  
  11. ----------------------------------------------------------  
  12. Plan hash value: 1601196873  
  13.   
  14. --------------------------------------------------------------------------  
  15. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  16. --------------------------------------------------------------------------  
  17. |   0 | SELECT STATEMENT  |      | 50815 |  1339K|    67   (2)| 00:00:01 |  
  18. |*  1 |  TABLE ACCESS FULL| T    | 50815 |  1339K|    67   (2)| 00:00:01 |  
  19. --------------------------------------------------------------------------  
  20.   
  21. Predicate Information (identified by operation id):  
  22. ---------------------------------------------------  
  23.   
  24.    1 - filter("ID"=99)  

      上面为补充内容,下面正式开始


 1、 sql的执行计划


 创建测试表

[sql] view plain copy
  1. SQL> create table t1(id int,name varchar2(1000));  
  2.    
  3. Table created  
  4.    
  5. SQL> create table t2(id int,name varchar2(1000));  
  6.    
  7. Table created  
  8.    
  9. SQL> create index ind_t1 on t1(id);  
  10.    
  11. Index created  
  12.    
  13. SQL> create index ind_t2 on t2(id);  
  14.    
  15. Index created  
  16.    
  17. SQL> create index ind_t2_name on t2(name);  
  18.    
  19. Index created  
  20.    
  21. SQL> insert into t1 select  a.OBJECT_ID,a.OBJECT_NAME from all_objects a;  
  22.    
  23. 50206 rows inserted  
  24.   
  25. SQL> insert into t2 select  a.OBJECT_ID,a.OBJECT_NAME from all_objects a where rownum<=20;  
  26.    
  27. 20 rows inserted  
  28.    
  29. SQL> commit;  
  30.    
  31. Commit complete  
  32.    
  33. SQL> exec dbms_stats.gather_table_stats(user,'t1',cascade => true);  
  34.    
  35. PL/SQL procedure successfully completed  
  36.   
  37. SQL> exec dbms_stats.gather_table_stats(user,'t2',cascade => true);  
  38.    
  39. PL/SQL procedure successfully completed  


2、产生执行计划

[sql] view plain copy
  1. SQL> select * from t1,t2 where t1.id= t2.id;  
  2.   
  3. 20 rows selected.  
  4.   
  5.   
  6. Execution Plan  
  7. ----------------------------------------------------------  
  8. Plan hash value: 828990364  
  9.   
  10. --------------------------------------------------------------------------------------  
  11. | Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
  12. --------------------------------------------------------------------------------------  
  13. |   0 | SELECT STATEMENT            |        |    20 |   780 |    43   (0)| 00:00:01 |  
  14. |   1 |  TABLE ACCESS BY INDEX ROWID| T1     |     1 |    28 |     2   (0)| 00:00:01 |  
  15. |   2 |   NESTED LOOPS              |        |    20 |   780 |    43   (0)| 00:00:01 |  
  16. |   3 |    TABLE ACCESS FULL        | T2     |    20 |   220 |     3   (0)| 00:00:01 |  
  17. |*  4 |    INDEX RANGE SCAN         | IND_T1 |     1 |       |     1   (0)| 00:00:01 |  
  18. --------------------------------------------------------------------------------------  
  19.   
  20. Predicate Information (identified by operation id):  
  21. ---------------------------------------------------  
  22.   
  23.    4 - access("T1"."ID"="T2"."ID")  
  24.   
  25.   
  26. Statistics  
  27. ----------------------------------------------------------  
  28.           1  recursive calls  
  29.           0  db block gets  
  30.          37  consistent gets  
  31.           0  physical reads  
  32.           0  redo size  
  33.        1452  bytes sent via SQL*Net to client  
  34.         503  bytes received via SQL*Net from client  
  35.           3  SQL*Net roundtrips to/from client  
  36.           0  sorts (memory)  
  37.           0  sorts (disk)  
  38.          20  rows processed  

         看执行计划时,我们首先从缩进最大的行读取,它是最先被执行的步骤。在执行计划中:id=3和id=4是最先被执行的,
[sql] view plain copy
  1. |   3 |    TABLE ACCESS FULL        | T2     |    20 |   220 |     3   (0)| 00:00:01 |  
  2. |*  4 |    INDEX RANGE SCAN         | IND_T1 |     1 |       |     1   (0)| 00:00:01 |  
          两行缩进一样的,最上面的最先被执行,在这里就是id=3
[sql] view plain copy
  1. |   3 |    TABLE ACCESS FULL        | T2     |    20 |   220 |     3   (0)| 00:00:01 |  
         选择次之缩进的行数id=2,表连接方式为NESTED LOOPS。
[sql] view plain copy
  1. |   2 |   NESTED LOOPS              |        |    20 |   780 |    43   (0)| 00:00:01 |    
        然后是id=1,扫描表的方式为TABLE ACCESS BY INDEX ROWID
[sql] view plain copy
  1. |   1 |  TABLE ACCESS BY INDEX ROWID| T1     |     1 |    28 |     2   (0)| 00:00:01 |  
       最后是id=0
[sql] view plain copy
  1. |   0 | SELECT STATEMENT            |        |    20 |   780 |    43   (0)| 00:00:01 |  
       我们翻译成语言大概如下,
      从t2表第一行读取,查看每一行是否符合下面条件:
 "T1"."ID"="T2"."ID"
       如果符合就拿出一行来,扫描整个t2表,这个过程就叫NESTED LOOPS
       当整个t2表被扫描完之后,会产生一个结果集,这个结果集是IND_T1的一个索引集,然后oracle根据索引键值上的rowid去T1表中找到相应的记录,就是这一步:TABLE ACCESS BY INDEX ROWID
        然后将结果返回:SELECT STATEMENT 
       id列为:id=3->id=4->id=2->id=1->id=0
让我们再看一看表中每一行表示什么含义:
1)Operation 列:当前操作的内容。
2)Rows 列 :就是当前操作的 cardinality ,Oracle估算当前操作的返回结果集。
3)Cost (%CPU) : Oracle计算出来的一个数值(代价),用于说明sql执行的代价。
4)Time 列:Oracle估算当前操作的时间。

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("T1"."ID"="T2"."ID")

这里有access和filter区别,access就表示这个谓词的条件的值将会影响数据的访问路径(一般针对索引),filter只起过滤作用。
举个例子
[sql] view plain copy
  1. SQL> select * from t1 where t1.name='AA';  
  2.   
  3. no rows selected  
  4.   
  5.   
  6. Execution Plan  
  7. ----------------------------------------------------------  
  8. Plan hash value: 3617692013  
  9.   
  10. --------------------------------------------------------------------------  
  11. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  12. --------------------------------------------------------------------------  
  13. |   0 | SELECT STATEMENT  |      |     2 |    56 |    69   (2)| 00:00:01 |  
  14. |*  1 |  TABLE ACCESS FULL| T1   |     2 |    56 |    69   (2)| 00:00:01 |  
  15. --------------------------------------------------------------------------  
  16.   
  17. Predicate Information (identified by operation id):  
  18. ---------------------------------------------------  
  19.   
  20.    1 - filter("T1"."NAME"='AA')  

懂了吧。

下面我们来仔细分析Operation里面的内容

[sql] view plain copy
  1. <pre name="code" class="sql"><p></p><p><strong>a、表访问方式</strong></p><p><strong><span style="font-family:宋体;color:#333333;font-size:14px; line-height:26px"><strong><span style="font-family:宋体;font-size:9pt">1.Full Table Scan (FTS) </span><span style="font-family:宋体;font-size:9pt">全表扫描</span></strong></span>  
  2. </strong></p><p>In a FTS operation, the whole table is read up to the high water mark (HWM). The HWM marks the last block in the table that has ever had data written to it. If you have deleted all the rows then you will still read up to the HWM. Truncate resets the HWM back to the start of the table. FTS uses multiblock i/o to read the blocks from disk.  <span style="color:#ff0000;"> </span><span style="color:#ff0000;">--全表扫描模式下会读数据到表的高水位线(HWM即表示表曾经扩展的最后一个数据块),读取速度依赖于Oracle初始化参db_block_multiblock_read_count(我觉得应该这样翻译:FTS扫描会使表使用上升到高水位(HWM),HWM标识了表最后写入数据的块,如果你用DELETE删除了所有的数据表仍然处于高水位(HWM),只有用TRUNCATE才能使表回归,FTS使用多IO从磁盘读取数据块)</span>.</p><p>Query Plan  
  3. ------------------------------------  
  4. SELECT STATEMENT [CHOOSE] Cost=1  
  5. **INDEX UNIQUE SCAN EMP_I1   <span style="color:#ff0000;">--如果索引里就找到了所要的数据,就不会再去访问表</span></p><p><span style="color:#ff0000;">  
  6. </span><strong>2.Index Lookup 索引扫描</strong>  
  7. There are 5 methods of index lookup:  
  8. <strong>  
  9. </strong></p><p><strong>1)index unique scan   --索引唯一扫描</strong>  
  10. Method for looking up a single key value via a unique index. always returns a single value, You must supply AT LEAST the leading column of the index to access data via the index.  
  11. eg:SQL> select empno,ename from emp where empno=10;</p><p></p><pre name="code" class="sql">SQL> select empno,ename from emp where empno=10;  
  12.   
  13. no rows selected  
  14.   
  15.   
  16. Execution Plan  
  17. ----------------------------------------------------------  
  18. Plan hash value: 2949544139  
  19.   
  20. --------------------------------------------------------------------------------------  
  21. | Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
  22. --------------------------------------------------------------------------------------  
  23. |   0 | SELECT STATEMENT            |        |     1 |    20 |     1   (0)| 00:00:01 |  
  24. |   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    20 |     1   (0)| 00:00:01 |  
  25. |*  2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     1   (0)| 00:00:01 |  
  26. --------------------------------------------------------------------------------------  
  27.   
  28. Predicate Information (identified by operation id):  
  29. ---------------------------------------------------  
  30.   
  31.    2 - access("EMPNO"=10)  
  32.   
  33.   
  34. Statistics  
  35. ----------------------------------------------------------  
  36.          24  recursive calls  
  37.           0  db block gets  
  38.           3  consistent gets  
  39.           0  physical reads  
  40.           0  redo size  
  41.         385  bytes sent via SQL*Net to client  
  42.         481  bytes received via SQL*Net from client  
  43.           1  SQL*Net roundtrips to/from client  
  44.           0  sorts (memory)  
  45.           0  sorts (disk)  
  46.           0  rows processed  
  47. </pre> <br>  
  48. <strong>2)index range scan   --索引局部扫描</strong><br>  
  49. Index range scan is a method for accessing a range values of a particular columnAT LEAST the leading column of the index must be supplied to access data via the index. Can be used for range operations (e.g. > < <> >= <= between) .<br>  
  50. <pre name="code" class="sql">SQL> select empno from emp where EMPNO>=7902;  
  51.   
  52.   
  53. Execution Plan  
  54. ----------------------------------------------------------  
  55. Plan hash value: 1567865628  
  56.   
  57. ---------------------------------------------------------------------------  
  58. | Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
  59. ---------------------------------------------------------------------------  
  60. |   0 | SELECT STATEMENT |        |     2 |    26 |     2   (0)| 00:00:01 |  
  61. |*  1 |  INDEX RANGE SCAN| PK_EMP |     2 |    26 |     2   (0)| 00:00:01 |  
  62. ---------------------------------------------------------------------------  
  63.   
  64. Predicate Information (identified by operation id):  
  65. ---------------------------------------------------  
  66.   
  67.    1 - access("EMPNO">=7902)  
  68.   
  69. Note  
  70. -----  
  71.    - dynamic sampling used for this statement  
  72.   
  73.   
  74. Statistics  
  75. ----------------------------------------------------------  
  76.           0  recursive calls  
  77.           0  db block gets  
  78.           2  consistent gets  
  79.           0  physical reads  
  80.           0  redo size  
  81.         569  bytes sent via SQL*Net to client  
  82.         492  bytes received via SQL*Net from client  
  83.           2  SQL*Net roundtrips to/from client  
  84.           0  sorts (memory)  
  85.           0  sorts (disk)  
  86.           2  rows processed  
  87. </pre><br>  
  88. <br>  
  89. <strong>3)index full scan   --索引全局扫描</strong><br>  
  90. Full index scans are only available in the CBO as otherwise we are unable to determine whether a full scan would be a good idea or not. We choose an index Full Scan when we have statistics that indicate that it is going to be more efficient than a Full table  
  91.  scan and a sort. For example we may do a Full index scan when we do an unbounded scan of an index and want the data to be ordered in the index order.<br>  
  92. <pre name="code" class="sql">SQL> select empno from emp order by empno;  
  93.   
  94. 14 rows selected.  
  95.   
  96.   
  97. Execution Plan  
  98. ----------------------------------------------------------  
  99. Plan hash value: 179099197  
  100.   
  101. ---------------------------------------------------------------------------  
  102. | Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
  103. ---------------------------------------------------------------------------  
  104. |   0 | SELECT STATEMENT |        |    14 |   182 |     2   (0)| 00:00:01 |  
  105. |   1 |  INDEX FULL SCAN | PK_EMP |    14 |   182 |     2   (0)| 00:00:01 |  
  106. ---------------------------------------------------------------------------  
  107.   
  108. Note  
  109. -----  
  110.    - dynamic sampling used for this statement  
  111.   
  112.   
  113. Statistics  
  114. ----------------------------------------------------------  
  115.           4  recursive calls  
  116.           0  db block gets  
  117.          11  consistent gets  
  118.           0  physical reads  
  119.           0  redo size  
  120.         676  bytes sent via SQL*Net to client  
  121.         492  bytes received via SQL*Net from client  
  122.           2  SQL*Net roundtrips to/from client  
  123.           0  sorts (memory)  
  124.           0  sorts (disk)  
  125.          14  rows processed  
  126. </pre><br>  
  127.  <br>  
  128. <strong>4)index fast full scan   --索引快速全局扫描,不带order by情况下常发生</strong><br>  
  129. Scans all the block in the indexRows are not returned in sorted order, Introduced in 7.3 and requires V733_PLANS_ENABLED=TRUE and CBO, may be hinted using INDEX_FFS hint, uses multiblock i/o, can be executed in parallel, can be used to access second column  
  130.  of concatenated indexes. This is because we are selecting all of the index.<br>  
  131. <pre name="code" class="sql">SQL> select empno from emp;  
  132.   
  133. 14 rows selected.  
  134.   
  135.   
  136. Execution Plan  
  137. ----------------------------------------------------------  
  138. Plan hash value: 366039554  
  139.   
  140. -------------------------------------------------------------------------------  
  141. | Id  | Operation            | Name   | Rows  | Bytes | Cost (%CPU)| Time     |  
  142. -------------------------------------------------------------------------------  
  143. |   0 | SELECT STATEMENT     |        |    14 |   182 |     2   (0)| 00:00:01 |  
  144. |   1 |  INDEX FAST FULL SCAN| PK_EMP |    14 |   182 |     2   (0)| 00:00:01 |  
  145. -------------------------------------------------------------------------------  
  146.   
  147. Note  
  148. -----  
  149.    - dynamic sampling used for this statement  
  150.   
  151.   
  152. Statistics  
  153. ----------------------------------------------------------  
  154.           4  recursive calls  
  155.           0  db block gets  
  156.          13  consistent gets  
  157.           0  physical reads  
  158.           0  redo size  
  159.         676  bytes sent via SQL*Net to client  
  160.         492  bytes received via SQL*Net from client  
  161.           2  SQL*Net roundtrips to/from client  
  162.           0  sorts (memory)  
  163.           0  sorts (disk)  
  164.          14  rows processed  
  165. </pre><br>  
  166. <strong>5)index skip scan   --索引跳跃扫描,where条件列是非索引的前导列情况下常发生</strong><br>  
  167. Index skip scan finds rows even if the column is not the leading column of a concatenated index. It skips the first column(s) during the search.<br>  
  168. <pre name="code" class="sql">SQL> create index i_emp on emp(empno, ename);  
  169.   
  170. Index created.  
  171. SQL> select /*+ index_ss(emp i_emp)*/ job from emp where ename='SMITH';  
  172.   
  173.   
  174. Execution Plan  
  175. ----------------------------------------------------------  
  176. Plan hash value: 98078853  
  177.   
  178. -------------------------------------------------------------------------------------  
  179. | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |  
  180. -------------------------------------------------------------------------------------  
  181. |   0 | SELECT STATEMENT            |       |     1 |    13 |     5   (0)| 00:00:01 |  
  182. |   1 |  TABLE ACCESS BY INDEX ROWID| EMP   |     1 |    13 |     5   (0)| 00:00:01 |  
  183. |*  2 |   INDEX SKIP SCAN           | I_EMP |     1 |       |     4   (0)| 00:00:01 |  
  184. -------------------------------------------------------------------------------------  
  185.   
  186. Predicate Information (identified by operation id):  
  187. ---------------------------------------------------  
  188.   
  189.    2 - access("ENAME"='SMITH')  
  190.        filter("ENAME"='SMITH')  
  191.   
  192. Note  
  193. -----  
  194.    - dynamic sampling used for this statement  
  195.   
  196.   
  197. Statistics  
  198. ----------------------------------------------------------  
  199.           5  recursive calls  
  200.           0  db block gets  
  201.          11  consistent gets  
  202.           0  physical reads  
  203.           0  redo size  
  204.         513  bytes sent via SQL*Net to client  
  205.         492  bytes received via SQL*Net from client  
  206.           2  SQL*Net roundtrips to/from client  
  207.           0  sorts (memory)  
  208.           0  sorts (disk)  
  209.           1  rows processed  
  210. </pre><br>  
  211. <p></p>  
  212. <p></p>  
  213. <p><strong>3.Rowid 物理ID扫描</strong><br>  
  214. This is the quickest access method available.Oracle retrieves the specified block and extracts the rows it is interested in. <br>  
  215. --Rowid扫描是最快的访问数据方式</p>  
  216. <p></p>  
  217. <pre name="code" class="sql">SQL> select * from emp where rowid='AAAjFUAAEAAABZ1AAM';  
  218.   
  219.   
  220. Execution Plan  
  221. ----------------------------------------------------------  
  222. Plan hash value: 1116584662  
  223.   
  224. -----------------------------------------------------------------------------------  
  225. | Id  | Operation                  | Name | Rows  | Bytes | Cost (%CPU)| Time     |  
  226. -----------------------------------------------------------------------------------  
  227. |   0 | SELECT STATEMENT           |      |     1 |    99 |     1   (0)| 00:00:01 |  
  228. |   1 |  TABLE ACCESS BY USER ROWID| EMP  |     1 |    99 |     1   (0)| 00:00:01 |  
  229. -----------------------------------------------------------------------------------  
  230.   
  231.   
  232. Statistics  
  233. ----------------------------------------------------------  
  234.           1  recursive calls  
  235.           0  db block gets  
  236.           1  consistent gets  
  237.           0  physical reads  
  238.           0  redo size  
  239.         983  bytes sent via SQL*Net to client  
  240.         492  bytes received via SQL*Net from client  
  241.           2  SQL*Net roundtrips to/from client  
  242.           0  sorts (memory)  
  243.           0  sorts (disk)  
  244.           1  rows processed  
  245.   
  246. </pre><br>  
  247. <br>  
  248. <p></p>  
  249. <p><br>  
  250. </p>  
  251. <p><strong>b、运算符</strong><br>  
  252. 1.sort    --排序,很消耗资源<br>  
  253. There are a number of different operations that promote sorts:<br>  
  254. (1)order by clauses (2)group by (3)sort merge join –-这三个会产生排序运算<br>  
  255.  <br>  
  256. 2.filter    --过滤,如not in、min函数等容易产生<br>  
  257. Has a number of different meanings, used to indicate partition elimination, may also indicate an actual filter step where one row source is filtering, another, functions such as min may introduce filter steps into query plans.<br>  
  258.  <br>  
  259. 3.view    --视图,大都由内联视图产生(可能深入到视图基表)<br>  
  260. When a view cannot be merged into the main query you will often see a projection view operation. This indicates that the 'view' will be selected from directly as opposed to being broken down into joins on the base tables. A number of constructs make a view  
  261.  non mergeable. Inline views are also non mergeable.<br>  
  262. <br>  
  263. </p>  
  264. <pre name="code" class="sql">SQL> select ename,tot from emp,(select empno,sum(empno) tot from emp group by empno) tmp where emp.empno = tmp.empno;  
  265.   
  266. 14 rows selected.  
  267.   
  268.   
  269. Execution Plan  
  270. ----------------------------------------------------------  
  271. Plan hash value: 138960760  
  272.   
  273. -----------------------------------------------------------------------------------------  
  274. | Id  | Operation                    | Name     | Rows  | Bytes | Cost (%CPU)| Time     |  
  275. -----------------------------------------------------------------------------------------  
  276. |   0 | SELECT STATEMENT             |          |    14 |   644 |     4  (25)| 00:00:01 |  
  277. |   1 |  MERGE JOIN                  |          |    14 |   644 |     4  (25)| 00:00:01 |  
  278. |   2 |   TABLE ACCESS BY INDEX ROWID| EMP      |    14 |   280 |     2   (0)| 00:00:01 |  
  279. |   3 |    INDEX FULL SCAN           | PK_EMPNO |    14 |       |     1   (0)| 00:00:01 |  
  280. |*  4 |   SORT JOIN                  |          |    14 |   364 |     2  (50)| 00:00:01 |  
  281. |   5 |    VIEW                      |          |    14 |   364 |     1   (0)| 00:00:01 |  
  282. |   6 |     HASH GROUP BY            |          |    14 |   182 |     1   (0)| 00:00:01 |  
  283. |   7 |      INDEX FULL SCAN         | PK_EMPNO |    14 |   182 |     1   (0)| 00:00:01 |  
  284. -----------------------------------------------------------------------------------------  
  285.   
  286. Predicate Information (identified by operation id):  
  287. ---------------------------------------------------  
  288.   
  289.    4 - access("EMP"."EMPNO"="TMP"."EMPNO")  
  290.        filter("EMP"."EMPNO"="TMP"."EMPNO")  
  291.   
  292. Note  
  293. -----  
  294.    - dynamic sampling used for this statement  
  295.   
  296.   
  297. Statistics  
  298. ----------------------------------------------------------  
  299.          43  recursive calls  
  300.           0  db block gets  
  301.          61  consistent gets  
  302.           0  physical reads  
  303.           0  redo size  
  304.         821  bytes sent via SQL*Net to client  
  305.         492  bytes received via SQL*Net from client  
  306.           2  SQL*Net roundtrips to/from client  
  307.           5  sorts (memory)  
  308.           0  sorts (disk)  
  309.          14  rows processed  
  310. </pre><br>  
  311. <br>  
  312.  <br>  
  313. 4.partition view     --分区视图<br>  
  314. Partition views are a legacy technology that were superceded by the partitioning option. This section of the article is provided as reference for such legacy systems.<br>  
  315. <br>  
  316. <br>  
  317. <p><strong>3、让我们再看看统计信息部分</strong></p>  
  318. <pre name="code" class="sql">SQL> set autotrace traceonly;  
  319. SQL> select count(*) from emp;  
  320.   
  321.   
  322. Execution Plan  
  323. ----------------------------------------------------------  
  324. Plan hash value: 2083865914  
  325.   
  326. -------------------------------------------------------------------  
  327. | Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |  
  328. -------------------------------------------------------------------  
  329. |   0 | SELECT STATEMENT   |      |     1 |     3   (0)| 00:00:01 |  
  330. |   1 |  SORT AGGREGATE    |      |     1 |            |          |  
  331. |   2 |   TABLE ACCESS FULL| EMP  |    14 |     3   (0)| 00:00:01 |  
  332. -------------------------------------------------------------------  
  333.   
  334. Note  
  335. -----  
  336.    - dynamic sampling used for this statement  
  337.   
  338. Statistics  
  339. ----------------------------------------------------------  
  340.           5  recursive calls  (归调用次数)  
  341.           0  db block gets  (从磁盘上读取的块数,即通过update/delete/select for update读的次数)  
  342.          15  consistent gets (从内存里读取的块数,即通过不带for updateselect 读的次数)  
  343.           0  physical reads (物理读—从磁盘读到数据块数量,一般来说是'consistent gets' + 'db block gets')  
  344.           0  redo size (重做数——执行SQL的过程中,产生的重做日志的大小)  
  345.         515  bytes sent via SQL*Net to client  
  346.         492  bytes received via SQL*Net from client  
  347.           2  SQL*Net roundtrips to/from client  
  348.           0  sorts (memory) (在内存中发生的排序)  
  349.           0  sorts (disk) (在硬盘中发生的排序)  
  350.           1  rows processed  
  351. <pre name="code" class="sql">说明:<pre name="code" class="sql"><span style="color:#CC0000;">Cost=(Single block I/O cost+ Multiblock I/O cost+   CPU cost)/sreadtim</span></pre>  
  352. <table class=" " border="0" cellpadding="0" cellspacing="0" height="331" width="611">  
  353. <colgroup><col width="90"><col width="257"><col width="461"></colgroup>  
  354. <tbody>  
  355. <tr height="38">  
  356. <td>  
  357. <p>序号</p>  
  358. </td>  
  359. <td>  
  360. <p>列名</p>  
  361. </td>  
  362. <td>  
  363. <p>解释</p>  
  364. </td>  
  365. </tr>  
  366. <tr height="56">  
  367. <td>  
  368. <p>1</p>  
  369. </td>  
  370. <td>  
  371. <p>db block gets</p>  
  372. </td>  
  373. <td>  
  374. <p>从buffer cache中读取的block的数量</p>  
  375. </td>  
  376. </tr>  
  377. <tr height="38">  
  378. <td>  
  379. <p>2</p>  
  380. </td>  
  381. <td>  
  382. <p>consistent gets</p>  
  383. </td>  
  384. <td>  
  385. <p>从buffer cache中读取的undo数据的block的数量</p>  
  386. </td>  
  387. </tr>  
  388. <tr height="18">  
  389. <td>  
  390. <p>3</p>  
  391. </td>  
  392. <td>  
  393. <p>physical reads</p>  
  394. </td>  
  395. <td>  
  396. <p>从磁盘读取的block的数量</p>  
  397. </td>  
  398. </tr>  
  399. <tr height="56">  
  400. <td>  
  401. <p>4</p>  
  402. </td>  
  403. <td>  
  404. <p>redo size</p>  
  405. </td>  
  406. <td>  
  407. <p>DML生成的redo的大小</p>  
  408. </td>  
  409. </tr>  
  410. <tr height="56">  
  411. <td>  
  412. <p>5</p>  
  413. </td>  
  414. <td>  
  415. <p>sorts (memory)</p>  
  416. </td>  
  417. <td>  
  418. <p>在内存执行的排序量</p>  
  419. </td>  
  420. </tr>  
  421. <tr height="56">  
  422. <td>  
  423. <p>6</p>  
  424. </td>  
  425. <td>  
  426. <p>sorts (disk)</p>  
  427. </td>  
  428. <td>  
  429. <p>在磁盘上执行的排序量</p>  
  430. </td>  
  431. </tr>  
  432. </tbody>  
  433. </table>  
  434. <br>  
  435. <p><br>  
  436. </p>  
  437. <pre></pre>  
  438. <pre></pre>  
  439. <br>  
  440. <p></p>  
  441. <pre></pre>  
  442. <pre></pre>  
  443. <pre></pre>  
  444. <pre></pre>  
  445. <pre></pre>  
  446. <pre></pre>  
  447. <pre></pre>  
  448. <pre></pre>  
  449. <pre></pre>  
  450. <pre></pre>  
  451. <pre></pre>  
  452. <pre></pre>  
  453.      

  1. </pre></pre></pre>  



原创粉丝点击