Oracle执行分析详细(一)

2014-11-24 16:18:24 · 作者: · 浏览: 5

我们以下面的一个例子来讲解

这里做个补充:trace的类型一共有以下几种

序号

命令

解释

1

SET AUTOTRACE OFF

此为默认值,即关闭Autotrace

2

SET AUTOTRACE ON EXPLAIN

只显示执行计划

3

SET AUTOTRACE ON STATISTICS

只显示执行的统计信息

4

SET AUTOTRACE ON

包含2,3两项内容

5

SET AUTOTRACE TRACEONLY

与ON相似,但不显示语句的执行结果

我喜欢SET AUTOTRACE TRACEONLY,我们以后的例子都是基于这种方式的

[sql] view plaincopyprint
  1. SQL> select * from departments a where a.department_id in (select b.department_id from employees b where b.employee_id=205);
  2. Execution Plan
  3. ---------------------------------------------------------- Plan hash value: 2782876085
  4. ----------------------------------------------------------------------------------------------
  5. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------------
  6. | 0 | SELECT STATEMENT | | 1 | 27 | 2 (0)| 00:00:01 | | 1 | NESTED LOOPS | | 1 | 27 | 2 (0)| 00:00:01 |
  7. | 2 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 7 | 1 (0)| 00:00:01 | |* 3 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK | 1 | | 0 (0)| 00:00:01 |
  8. | 4 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 | 540 | 1 (0)| 00:00:01 | |* 5 | INDEX UNIQUE SCAN | DEPT_ID_PK | 1 | | 0 (0)| 00:00:01 |
  9. ----------------------------------------------------------------------------------------------
  10. Predicate Information (identified by operation id): ---------------------------------------------------
  11. 3 - access("B"."EMPLOYEE_ID"=205)
  12. 5 - access("A"."DEPARTMENT_ID"="B"."DEPARTMENT_ID")
  13. Statistics
  14. ---------------------------------------------------------- 1 recursive calls
  15. 0 db block gets 4 consistent gets
  16. 0 physical reads 0 redo size
  17. 749 bytes sent via SQL*Net to client 492 bytes received via SQL*Net from client
  18. 2 SQL*Net roundtrips to/from client 0 sorts (memory)
  19. 0 sorts (disk) 1 rows processed
  20. SQL> select * from departments a where a.department_id in (select b.department_id from employees b where b.employee_id=205);
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 2782876085
    
    ----------------------------------------------------------------------------------------------
    | Id  | Operation                    | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT             |               |     1 |    27 |     2   (0)| 00:00:01 |
    |   1 |  NESTED LOOPS                |               |     1 |    27 |     2   (0)| 00:00:01 |
    |   2 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES     |     1 |     7 |     1   (0)| 00:00:01 |
    |*  3 |    INDEX UNIQUE SCAN         | EMP_EMP_ID_PK |     1 |       |     0   (0)| 00:00:01 |
    |   4 |   TABLE ACCESS BY INDEX ROWID| DEPARTMENTS   |    27 |   540 |     1   (0)| 00:00:01 |
    |*  5 |    INDEX UNIQUE SCAN         | DEPT_ID_PK    |     1 |       |     0   (0)| 00:00:01 |
    ----------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       3 - access("B"."EMPLOYEE_ID"=205)
       5 - access("A"."DEPARTMENT_ID"="B"."DEPARTMENT_ID")
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
              4  consistent gets
              0  physical reads
              0  redo size
            749  bytes sent via SQL*Net to client
            492  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processed
     
    让我们来一行一行的看:

    一、表部分

    1、Plan hash value:

    [sql] view plaincopyprin