2013-08-06

--------------解释执行计划的方法----------------------------


用autotrace前提:


SQL> @/u01/oracle/product/10.2.0/db_1/sqlplus/admin/plustrce.sql  --sys用户下执行

SQL>

SQL> drop role plustrace;

drop role plustrace

         *

ERROR at line 1:

ORA-01919: role 'PLUSTRACE' does not exist



SQL> create role plustrace;


Role created.


SQL> grant select on v_$sesstat to plustrace;


Grant succeeded.


SQL> grant select on v_$statname to plustrace;


Grant succeeded.


SQL> grant select on v_$mystat to plustrace;


Grant succeeded.


SQL> grant plustrace to dba with admin option;


Grant succeeded.


SQL> set echo off


SQL> grant plustrace to scott;  --将权限授予SCOTT可以用autotrace


Grant succeeded.


-------------------------------------------------------------


1、autotrace工具详解


SQL> set autotrace off   ——默认情况下是关闭的


SQL> set autotrace on exp         --显示查询结果、执行计划

SQL> select * from t where rownum=1;


OWNER                          OBJECT_TYPE

------------------------------ -------------------

PUBLIC                         SYNONYM



Execution Plan

----------------------------------------------------------

Plan hash value: 508354683


---------------------------------------------------------------------------

| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |     1 |    28 |     2   (0)| 00:00:01 |

|*  1 |  COUNT STOPKEY     |      |       |       |            |          |

|   2 |   TABLE ACCESS FULL| T    | 45659 |  1248K|     2   (0)| 00:00:01 |

---------------------------------------------------------------------------


Predicate Information (identified by operation id):

---------------------------------------------------


  1 - filter(ROWNUM=1)


Note

-----

  - dynamic sampling used for this statement


SQL> set autotrace on            --显示查询结果、执行计划、执行的物理统计信息

SQL> select * from t where rownum=1;


OBJECT_NAME                    OBJECT_TYPE

------------------------------ -------------------

DUAL                           TABLE



Execution Plan

----------------------------------------------------------

Plan hash value: 508354683


---------------------------------------------------------------------------

| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |     1 |    28 |     2   (0)| 00:00:01 |

|*  1 |  COUNT STOPKEY     |      |       |       |            |          |

|   2 |   TABLE ACCESS FULL| T    | 43751 |  1196K|     2   (0)| 00:00:01 |

---------------------------------------------------------------------------


Predicate Information (identified by operation id):

---------------------------------------------------


  1 - filter(ROWNUM=1)


Note

-----

  - dynamic sampling used for this statement



Statistics

----------------------------------------------------------

        71  recursive calls

         0  db block gets

       146  consistent gets

       155  physical reads

         0  redo size

       483  bytes sent via SQL*Net to client

       400  bytes received via SQL*Net from client

         2  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         1  rows processed



SQL> set autotrace on stat   --显示查询结果、执行的物理统计信息

SQL> select * from t where rownum=1;


OBJECT_NAME                    OBJECT_TYPE

------------------------------ -------------------

DUAL                           TABLE



Statistics

----------------------------------------------------------

         0  recursive calls

         0  db block gets

         4  consistent gets

         0  physical reads

         0  redo size

       483  bytes sent via SQL*Net to client

       400  bytes received via SQL*Net from client

         2  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         1  rows processed


SQL> set autotrace trace exp   --显示执行计划

SQL> select * from t where rownum=1;


Execution Plan

----------------------------------------------------------

Plan hash value: 508354683


---------------------------------------------------------------------------

| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |     1 |    28 |     2   (0)| 00:00:01 |

|*  1 |  COUNT STOPKEY     |      |       |       |            |          |

|   2 |   TABLE ACCESS FULL| T    | 43751 |  1196K|     2   (0)| 00:00:01 |

---------------------------------------------------------------------------


Predicate Information (identified by operation id):

---------------------------------------------------


  1 - filter(ROWNUM=1)


Note

-----

  - dynamic sampling used for this statement


SQL> set autotrace trace exp stat  --显示执行计划和物理统计信息

SQL> select * from t where rownum=1;



Execution Plan

----------------------------------------------------------

Plan hash value: 508354683


---------------------------------------------------------------------------

| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |     1 |    28 |     2   (0)| 00:00:01 |

|*  1 |  COUNT STOPKEY     |      |       |       |            |          |

|   2 |   TABLE ACCESS FULL| T    | 43751 |  1196K|     2   (0)| 00:00:01 |

---------------------------------------------------------------------------


Predicate Information (identified by operation id):

---------------------------------------------------


  1 - filter(ROWNUM=1)


Note

-----

  - dynamic sampling used for this statement



Statistics

----------------------------------------------------------

         0  recursive calls

         0  db block gets

         4  consistent gets

         0  physical reads

         0  redo size

       483  bytes sent via SQL*Net to client

       400  bytes received via SQL*Net from client

         2  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         1  rows processed


SQL> set autotrace trace stat   --显示物理统计信息

SQL> select * from t where rownum=1;



Statistics

----------------------------------------------------------

         0  recursive calls

         0  db block gets

         4  consistent gets

         0  physical reads

         0  redo size

       483  bytes sent via SQL*Net to client

       400  bytes received via SQL*Net from client

         2  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         1  rows processed


---------------------------------------------------------------


总结:

set autotrace on                = set autotrace on     exp stat

set autotrace on     exp stat

             on     exp

             on     stat

             trace             = trace  exp stat

             trace  exp stat

             trace  exp

             trace  stat


只要有on,一定会显示数据,只要有trace,就不显示数据。



--------------------------------------------------------------------


2、dbms_xplan  --通过内部的包,仅仅显示执行计划


解释执行计划

SQL> explain plan for select * from t where rownum=1;


Explained.


查看执行计划


SQL> select * from table(dbms_xplan.display);


PLAN_TABLE_OUTPUT

--------------------------------------------------------------------------------

Plan hash value: 508354683


---------------------------------------------------------------------------

| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |     1 |    28 |     2   (0)| 00:00:01 |

|*  1 |  COUNT STOPKEY     |      |       |       |            |          |

|   2 |   TABLE ACCESS FULL| T    | 43751 |  1196K|     2   (0)| 00:00:01 |

---------------------------------------------------------------------------


Predicate Information (identified by operation id):


PLAN_TABLE_OUTPUT

--------------------------------------------------------------------------------


  1 - filter(ROWNUM=1)


Note

-----

  - dynamic sampling used for this statement


18 rows selected.