3.解釋執行計劃的方法

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.





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章