Orace执行计划分析说明

来源:互联网 发布:战舰世界鞍山数据 编辑:程序博客网 时间:2024/06/06 09:59
1、创建测试表
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. SQL> create table t as select 1 id,object_name from dba_objects;    
  3.      
  4. Table created    
  5.      
  6. SQL> update t set id=99 where rownum=1;    
  7.      
  8. 1 row updated    
  9.      
  10. SQL> commit;    
  11.      
  12. Commit complete    
  13.      
  14. SQL> create index t_ind on t(id);    
  15.      
  16. Index created    

        oracle优化器:RBO和CBO两种, 从oracle10g开始优化器已经抛弃了RBO,下面的列子说明CBO大概是怎样的
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. SQL>  select /*+dynamic_sampling(t 0) */* from t where id=1;    
  3.     
  4. 50819 rows selected.    
  5.     
  6.     
  7. Execution Plan    
  8. ----------------------------------------------------------    
  9. Plan hash value: 1376202287    
  10.     
  11. -------------------------------------------------------------------------------------    
  12. | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |    
  13. -------------------------------------------------------------------------------------    
  14. |   0 | SELECT STATEMENT            |       |   195 | 15405 |    51   (0)| 00:00:01 |    
  15. |   1 |  TABLE ACCESS BY INDEX ROWID| T     |   195 | 15405 |    51   (0)| 00:00:01 |    
  16. |*  2 |   INDEX RANGE SCAN          | T_IND |    78 |       |    50   (0)| 00:00:01 |    
  17. -------------------------------------------------------------------------------------    
  18.     
  19. Predicate Information (identified by operation id):    
  20. ---------------------------------------------------   
  21.     
  22.    2 - access("ID"=1)     

       现象t表还没有被分析,提示/*+dynamic_sampling(t 0) */*的目的是让CBO无法通过动态采样获取表中的实际数据情况,此时CBO只能根据T表中非常有限的信息(比如表中的extents数量,数据块的数量)来猜测表中的数据。从结果中可以看到CBO猜出表中id=1的有195条,这个数值对于表的总数来说,是一个非常小的值,所以CBO选择了索引而不是全表扫描。
      而实际情况如下所示:
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. SQL> select * from  t where id=1    
  3.   2  ;    
  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  |      | 49454 |  3815K|    67   (2)| 00:00:01 |    
  16. |*  1 |  TABLE ACCESS FULL| T    | 49454 |  3815K|    67   (2)| 00:00:01 |    
  17. --------------------------------------------------------------------------    
  18.     
  19. Predicate Information (identified by operation id):    
  20. ---------------------------------------------------    
  21.     
  22.    1 - filter("ID"=1)    


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

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


现在扫描过的行数为50815。

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

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


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

       我们收集一把统计信息。

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

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

1、 sql的执行计划

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


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


         看执行计划时,我们首先从缩进最大的行读取,它是最先被执行的步骤。在执行计划中:id=3和id=4是最先被执行的,
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. |   3 |    TABLE ACCESS FULL        | T2     |    20 |   220 |     3   (0)| 00:00:01 |    
  3. |*  4 |    INDEX RANGE SCAN         | IND_T1 |     1 |       |     1   (0)| 00:00:01 |    

          两行缩进一样的,最上面的最先被执行,在这里就是id=3
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. |   3 |    TABLE ACCESS FULL        | T2     |    20 |   220 |     3   (0)| 00:00:01 |   

         选择次之缩进的行数id=2,表连接方式为NESTED LOOPS。
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. |   2 |   NESTED LOOPS              |        |    20 |   780 |    43   (0)| 00:00:01 |   
  
        然后是id=1,扫描表的方式为TABLE ACCESS BY INDEX ROWID
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. |   1 |  TABLE ACCESS BY INDEX ROWID| T1     |     1 |    28 |     2   (0)| 00:00:01 |  
 
       最后是id=0
Sql代码  收藏代码
  1. [sql] view plaincopy  
  2. |   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代码  收藏代码
  1. [sql] view plaincopy  
  2. SQL> select * from t1 where t1.name='AA';    
  3.     
  4. no rows selected    
  5.     
  6.     
  7. Execution Plan    
  8. ----------------------------------------------------------    
  9. Plan hash value: 3617692013    
  10.     
  11. --------------------------------------------------------------------------    
  12. | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |    
  13. --------------------------------------------------------------------------    
  14. |   0 | SELECT STATEMENT  |      |     2 |    56 |    69   (2)| 00:00:01 |    
  15. |*  1 |  TABLE ACCESS FULL| T1   |     2 |    56 |    69   (2)| 00:00:01 |    
  16. --------------------------------------------------------------------------    
  17.     
  18. Predicate Information (identified by operation id):    
  19. ---------------------------------------------------    
  20.     
  21.    1 - filter("T1"."NAME"='AA')    


懂了吧。
下面我们来仔细分析Operation里面的内容
Sql代码  收藏代码
  1. [sql] view plaincopy   

a、表访问方式
1.Full Table Scan (FTS) 全表扫描

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. 
--全表扫描模式下会读数据到表的高水位线(HWM即表示表曾经扩展的最后一个数据块),读取速度依赖于Oracle初始化参db_block_multiblock_read_count(我觉得应该这样翻译:FTS扫描会使表使用上升到高水位(HWM),HWM标识了表最后写入数据的块,如果你用DELETE删除了所有的数据表仍然处于高水位(HWM),只有用TRUNCATE才能使表回归,FTS使用多IO从磁盘读取数据块)Query Plan 
------------------------------------ 
Sql代码  收藏代码
  1. SELECT STATEMENT [CHOOSE] Cost=1    
  2. **INDEX UNIQUE SCAN EMP_I1   

--如果索引里就找到了所要的数据,就不会再去访问表
2.Index Lookup 索引扫描
There are 5 methods of index lookup:   
1)index unique scan   --索引唯一扫描 
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.
eg:
Sql代码  收藏代码
  1. SQL> select empno,ename from emp where empno=10  
  2. SQL> select empno,ename from emp where empno=10;    
  3.     
  4. no rows selected    
  5.     
  6.     
  7. Execution Plan    
  8. ----------------------------------------------------------    
  9. Plan hash value: 2949544139    
  10.     
  11. --------------------------------------------------------------------------------------    
  12. | Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |    
  13. --------------------------------------------------------------------------------------    
  14. |   0 | SELECT STATEMENT            |        |     1 |    20 |     1   (0)| 00:00:01 |    
  15. |   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    20 |     1   (0)| 00:00:01 |    
  16. |*  2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     1   (0)| 00:00:01 |    
  17. --------------------------------------------------------------------------------------    
  18.     
  19. Predicate Information (identified by operation id):    
  20. ---------------------------------------------------    
  21.     
  22.    2 - access("EMPNO"=10)    
  23.     
  24.     
  25. Statistics    
  26. ----------------------------------------------------------    
  27.          24  recursive calls    
  28.           0  db block gets    
  29.           3  consistent gets    
  30.           0  physical reads    
  31.           0  redo size    
  32.         385  bytes sent via SQL*Net to client    
  33.         481  bytes received via SQL*Net from client    
  34.           1  SQL*Net roundtrips to/from client    
  35.           0  sorts (memory)    
  36.           0  sorts (disk)    
  37.           0  rows processed   


2)index range scan   --索引局部扫描
Index range scan is a method for accessing a range values of a particular column. AT LEAST the leading column of the index must be supplied to access data via the index. Can be used for range operations (e.g. >  
Sql代码  收藏代码
  1. SQL> select empno from emp where EMPNO>=7902;    
  2.     
  3.     
  4. Execution Plan    
  5. ----------------------------------------------------------    
  6. Plan hash value: 1567865628    
  7.     
  8. ---------------------------------------------------------------------------    
  9. | Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |    
  10. ---------------------------------------------------------------------------    
  11. |   0 | SELECT STATEMENT |        |     2 |    26 |     2   (0)| 00:00:01 |    
  12. |*  1 |  INDEX RANGE SCAN| PK_EMP |     2 |    26 |     2   (0)| 00:00:01 |    
  13. ---------------------------------------------------------------------------    
  14.     
  15. Predicate Information (identified by operation id):    
  16. ---------------------------------------------------    
  17.     
  18.    1 - access("EMPNO">=7902)    
  19.     
  20. Note    
  21. -----    
  22.    - dynamic sampling used for this statement    
  23.     
  24.     
  25. Statistics    
  26. ----------------------------------------------------------    
  27.           0  recursive calls    
  28.           0  db block gets    
  29.           2  consistent gets    
  30.           0  physical reads    
  31.           0  redo size    
  32.         569  bytes sent via SQL*Net to client    
  33.         492  bytes received via SQL*Net from client    
  34.           2  SQL*Net roundtrips to/from client    
  35.           0  sorts (memory)    
  36.           0  sorts (disk)    
  37.           2  rows processed    

3)index full scan   --索引全局扫描 
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 
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. 
Sql代码  收藏代码
  1. SQL> select empno from emp order by empno;    
  2.     
  3. 14 rows selected.    
  4.     
  5.     
  6. Execution Plan    
  7. ----------------------------------------------------------    
  8. Plan hash value: 179099197    
  9.     
  10. ---------------------------------------------------------------------------    
  11. | Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |    
  12. ---------------------------------------------------------------------------    
  13. |   0 | SELECT STATEMENT |        |    14 |   182 |     2   (0)| 00:00:01 |    
  14. |   1 |  INDEX FULL SCAN | PK_EMP |    14 |   182 |     2   (0)| 00:00:01 |    
  15. ---------------------------------------------------------------------------    
  16.     
  17. Note    
  18. -----    
  19.    - dynamic sampling used for this statement    
  20.     
  21.     
  22. Statistics    
  23. ----------------------------------------------------------    
  24.           4  recursive calls    
  25.           0  db block gets    
  26.          11  consistent gets    
  27.           0  physical reads    
  28.           0  redo size    
  29.         676  bytes sent via SQL*Net to client    
  30.         492  bytes received via SQL*Net from client    
  31.           2  SQL*Net roundtrips to/from client    
  32.           0  sorts (memory)    
  33.           0  sorts (disk)    
  34.          14  rows processed   

4)index fast full scan   --索引快速全局扫描,不带order by情况下常发生
Scans all the block in the index, Rows 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 
of concatenated indexes. This is because we are selecting all of the index. 
Sql代码  收藏代码
  1. SQL> select empno from emp;    
  2.     
  3. 14 rows selected.    
  4.     
  5.     
  6. Execution Plan    
  7. ----------------------------------------------------------    
  8. Plan hash value: 366039554    
  9.     
  10. -------------------------------------------------------------------------------    
  11. | Id  | Operation            | Name   | Rows  | Bytes | Cost (%CPU)| Time     |    
  12. -------------------------------------------------------------------------------    
  13. |   0 | SELECT STATEMENT     |        |    14 |   182 |     2   (0)| 00:00:01 |    
  14. |   1 |  INDEX FAST FULL SCAN| PK_EMP |    14 |   182 |     2   (0)| 00:00:01 |    
  15. -------------------------------------------------------------------------------    
  16.     
  17. Note    
  18. -----    
  19.    - dynamic sampling used for this statement    
  20.     
  21.     
  22. Statistics    
  23. ----------------------------------------------------------    
  24.           4  recursive calls    
  25.           0  db block gets    
  26.          13  consistent gets    
  27.           0  physical reads    
  28.           0  redo size    
  29.         676  bytes sent via SQL*Net to client    
  30.         492  bytes received via SQL*Net from client    
  31.           2  SQL*Net roundtrips to/from client    
  32.           0  sorts (memory)    
  33.           0  sorts (disk)    
  34.          14  rows processed   

5)index skip scan   --索引跳跃扫描,where条件列是非索引的前导列情况下常发生
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. 
Sql代码  收藏代码
  1. SQL> create index i_emp on emp(empno, ename);    
  2.     
  3. Index created.    
  4. SQL> select /*+ index_ss(emp i_emp)*/ job from emp where ename='SMITH';    
  5.     
  6.     
  7. Execution Plan    
  8. ----------------------------------------------------------    
  9. Plan hash value: 98078853    
  10.     
  11. -------------------------------------------------------------------------------------    
  12. | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |    
  13. -------------------------------------------------------------------------------------    
  14. |   0 | SELECT STATEMENT            |       |     1 |    13 |     5   (0)| 00:00:01 |    
  15. |   1 |  TABLE ACCESS BY INDEX ROWID| EMP   |     1 |    13 |     5   (0)| 00:00:01 |    
  16. |*  2 |   INDEX SKIP SCAN           | I_EMP |     1 |       |     4   (0)| 00:00:01 |    
  17. -------------------------------------------------------------------------------------    
  18.     
  19. Predicate Information (identified by operation id):    
  20. ---------------------------------------------------    
  21.     
  22.    2 - access("ENAME"='SMITH')    
  23.        filter("ENAME"='SMITH')    
  24.     
  25. Note    
  26. -----    
  27.    - dynamic sampling used for this statement    
  28.     
  29.     
  30. Statistics    
  31. ----------------------------------------------------------    
  32.           5  recursive calls    
  33.           0  db block gets    
  34.          11  consistent gets    
  35.           0  physical reads    
  36.           0  redo size    
  37.         513  bytes sent via SQL*Net to client    
  38.         492  bytes received via SQL*Net from client    
  39.           2  SQL*Net roundtrips to/from client    
  40.           0  sorts (memory)    
  41.           0  sorts (disk)    
  42.           1  rows processed    

3.Rowid 物理ID扫描
This is the quickest access method available.Oracle retrieves the specified block and extracts the rows it is interested in.  
--Rowid扫描是最快的访问数据方式
Sql代码  收藏代码
  1. SQL> select * from emp where rowid='AAAjFUAAEAAABZ1AAM';    
  2.     
  3.     
  4. Execution Plan    
  5. ----------------------------------------------------------    
  6. Plan hash value: 1116584662    
  7.     
  8. -----------------------------------------------------------------------------------    
  9. | Id  | Operation                  | Name | Rows  | Bytes | Cost (%CPU)| Time     |    
  10. -----------------------------------------------------------------------------------    
  11. |   0 | SELECT STATEMENT           |      |     1 |    99 |     1   (0)| 00:00:01 |    
  12. |   1 |  TABLE ACCESS BY USER ROWID| EMP  |     1 |    99 |     1   (0)| 00:00:01 |    
  13. -----------------------------------------------------------------------------------    
  14.     
  15.     
  16. Statistics    
  17. ----------------------------------------------------------    
  18.           1  recursive calls    
  19.           0  db block gets    
  20.           1  consistent gets    
  21.           0  physical reads    
  22.           0  redo size    
  23.         983  bytes sent via SQL*Net to client    
  24.         492  bytes received via SQL*Net from client    
  25.           2  SQL*Net roundtrips to/from client    
  26.           0  sorts (memory)    
  27.           0  sorts (disk)    
  28.           1  rows processed    

 
b、运算符
1.sort    --排序,很消耗资源

There are a number of different operations that promote sorts: 
(1)order by clauses (2)group by (3)sort merge join –-这三个会产生排序运算

2.filter    --过滤,如not in、min函数等容易产生
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.

3.view    --视图,大都由内联视图产生(可能深入到视图基表)
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 
non mergeable. Inline views are also non mergeable. 

Sql代码  收藏代码
  1. SQL> select ename,tot from emp,(select empno,sum(empno) tot from emp group by empno) tmp where emp.empno = tmp.empno;    
  2.     
  3. 14 rows selected.    
  4.     
  5.     
  6. Execution Plan    
  7. ----------------------------------------------------------    
  8. Plan hash value: 138960760    
  9.     
  10. -----------------------------------------------------------------------------------------    
  11. | Id  | Operation                    | Name     | Rows  | Bytes | Cost (%CPU)| Time     |    
  12. -----------------------------------------------------------------------------------------    
  13. |   0 | SELECT STATEMENT             |          |    14 |   644 |     4  (25)| 00:00:01 |    
  14. |   1 |  MERGE JOIN                  |          |    14 |   644 |     4  (25)| 00:00:01 |    
  15. |   2 |   TABLE ACCESS BY INDEX ROWID| EMP      |    14 |   280 |     2   (0)| 00:00:01 |    
  16. |   3 |    INDEX FULL SCAN           | PK_EMPNO |    14 |       |     1   (0)| 00:00:01 |    
  17. |*  4 |   SORT JOIN                  |          |    14 |   364 |     2  (50)| 00:00:01 |    
  18. |   5 |    VIEW                      |          |    14 |   364 |     1   (0)| 00:00:01 |    
  19. |   6 |     HASH GROUP BY            |          |    14 |   182 |     1   (0)| 00:00:01 |    
  20. |   7 |      INDEX FULL SCAN         | PK_EMPNO |    14 |   182 |     1   (0)| 00:00:01 |    
  21. -----------------------------------------------------------------------------------------    
  22.     
  23. Predicate Information (identified by operation id):    
  24. ---------------------------------------------------    
  25.     
  26.    4 - access("EMP"."EMPNO"="TMP"."EMPNO")    
  27.        filter("EMP"."EMPNO"="TMP"."EMPNO")    
  28.     
  29. Note    
  30. -----    
  31.    - dynamic sampling used for this statement    
  32.     
  33.     
  34. Statistics    
  35. ----------------------------------------------------------    
  36.          43  recursive calls    
  37.           0  db block gets    
  38.          61  consistent gets    
  39.           0  physical reads    
  40.           0  redo size    
  41.         821  bytes sent via SQL*Net to client    
  42.         492  bytes received via SQL*Net from client    
  43.           2  SQL*Net roundtrips to/from client    
  44.           5  sorts (memory)    
  45.           0  sorts (disk)    
  46.          14  rows processed    


4.partition view     --分区视图
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.
3、让我们再看看统计信息部分
Sql代码  收藏代码
  1. SQL> set autotrace traceonly;    
  2. SQL> select count(*) from emp;    
  3.     
  4.     
  5. Execution Plan    
  6. ----------------------------------------------------------    
  7. Plan hash value: 2083865914    
  8.     
  9. -------------------------------------------------------------------    
  10. | Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |    
  11. -------------------------------------------------------------------    
  12. |   0 | SELECT STATEMENT   |      |     1 |     3   (0)| 00:00:01 |    
  13. |   1 |  SORT AGGREGATE    |      |     1 |            |          |    
  14. |   2 |   TABLE ACCESS FULL| EMP  |    14 |     3   (0)| 00:00:01 |    
  15. -------------------------------------------------------------------    
  16.     
  17. Note    
  18. -----    
  19.    - dynamic sampling used for this statement    
  20.     
  21. Statistics    
  22. ----------------------------------------------------------    
  23.           5  recursive calls  (归调用次数)    
  24.           0  db block gets  (从磁盘上读取的块数,即通过update/delete/select for update读的次数)    
  25.          15  consistent gets (从内存里读取的块数,即通过不带for updateselect 读的次数)    
  26.           0  physical reads (物理读—从磁盘读到数据块数量,一般来说是'consistent gets' + 'db block gets')    
  27.           0  redo size (重做数——执行SQL的过程中,产生的重做日志的大小)    
  28.         515  bytes sent via SQL*Net to client    
  29.         492  bytes received via SQL*Net from client    
  30.           2  SQL*Net roundtrips to/from client    
  31.           0  sorts (memory) (在内存中发生的排序)    
  32.           0  sorts (disk) (在硬盘中发生的排序)    
  33.           1  rows processed    

说明:Cost=(Single block I/O cost+ Multiblock I/O cost+   CPU cost)/sreadtim

序号列名解释1db block gets从buffer cache中读取的block的数量2consistent gets从buffer cache中读取的undo数据的block的数量3physical reads从磁盘读取的block的数量4redo sizDML生成的redo的大小5sorts (memory)在内存执行的排序量6sorts (disk)在磁盘上执行的排序量
0 0