Oracle檢視執行計劃(三)

stonebox1122發表於2017-05-17

如果某條SQL已經執行過了,且其執行計劃還在記憶體中,則後續可以使用dbms.xplan.display_cursor來檢視。
語法:
select * from table(dbms_xplan.display_cursor('sql_id/hash_values',child_cursor_number,'advanced'));

SQL> select employee_id,last_name,salary from employees where employee_id=100;

EMPLOYEE_ID LAST_NAME                     SALARY
----------- ------------------------- ----------
        100 King                           24000


直接檢視剛剛執行過的SQL的執行計劃:
SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced'));

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID  ftsw1cm8qh6nq, child number 0
-------------------------------------
select employee_id,last_name,salary from employees where employee_id=100

Plan hash value: 1833546154

---------------------------------------------------------------------------------------------
| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |               |       |       |     1 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMPLOYEES     |     1 |    16 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | EMP_EMP_ID_PK |     1 |       |     0   (0)|          |
---------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1 / EMPLOYEES@SEL$1
   2 - SEL$1 / EMPLOYEES@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
      DB_VERSION('11.2.0.4')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$1")
      INDEX_RS_ASC(@"SEL$1" "EMPLOYEES"@"SEL$1" ("EMPLOYEES"."EMPLOYEE_ID"))
      END_OUTLINE_DATA
  */

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

   2 - access("EMPLOYEE_ID"=100)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "EMPLOYEE_ID"[NUMBER,22], "LAST_NAME"[VARCHAR2,25], "SALARY"[NUMBER,22]
   2 - "EMPLOYEES".ROWID[ROWID,10], "EMPLOYEE_ID"[NUMBER,22]


45 rows selected.

如果SQL已經執行過了,則需要透過先查詢V$SQL,找到該SQL的SQL_ID再來檢視執行計劃。
SQL> select sql_text,sql_id from v$sql where sql_text like 'select employee_id,last_name%';

SQL_TEXT                                                                         SQL_ID
-------------------------------------------------------------------------------- -------------
select employee_id,last_name,salary from employees where employee_id=100         ftsw1cm8qh6nq

SQL> select * from table(dbms_xplan.display_cursor('ftsw1cm8qh6nq',0,'advanced'));

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID  ftsw1cm8qh6nq, child number 0
-------------------------------------
select employee_id,last_name,salary from employees where employee_id=100

Plan hash value: 1833546154

---------------------------------------------------------------------------------------------
| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |               |       |       |     1 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMPLOYEES     |     1 |    16 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | EMP_EMP_ID_PK |     1 |       |     0   (0)|          |
---------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1 / EMPLOYEES@SEL$1
   2 - SEL$1 / EMPLOYEES@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
      DB_VERSION('11.2.0.4')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$1")
      INDEX_RS_ASC(@"SEL$1" "EMPLOYEES"@"SEL$1" ("EMPLOYEES"."EMPLOYEE_ID"))
      END_OUTLINE_DATA
  */

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

   2 - access("EMPLOYEE_ID"=100)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "EMPLOYEE_ID"[NUMBER,22], "LAST_NAME"[VARCHAR2,25], "SALARY"[NUMBER,22]
   2 - "EMPLOYEES".ROWID[ROWID,10], "EMPLOYEE_ID"[NUMBER,22]


45 rows selected.

如果目標SQL有多個執行計劃,需要檢視某個執行計劃,只需要指定目標SQL的child_cursor_number就可以了。
SQL> conn sh/sh
Connected.

SQL> create table employees as select * from hr.employees;

Table created.

SQL> select employee_id,last_name,salary from employees where employee_id=100;

EMPLOYEE_ID LAST_NAME                     SALARY
----------- ------------------------- ----------
        100 King                           24000

SQL> conn hr/hr
Connected.

SQL> select sql_text,sql_id,child_number from v$sql where sql_text like 'select employee_id,last_name%';

SQL_TEXT                                                                         SQL_ID        CHILD_NUMBER
-------------------------------------------------------------------------------- ------------- ------------
select employee_id,last_name,salary from employees where employee_id=100         ftsw1cm8qh6nq            0
select employee_id,last_name,salary from employees where employee_id=100         ftsw1cm8qh6nq            1

SQL> select * from table(dbms_xplan.display_cursor('ftsw1cm8qh6nq',1,'advanced'));

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID  ftsw1cm8qh6nq, child number 1
-------------------------------------
select employee_id,last_name,salary from employees where employee_id=100

Plan hash value: 1445457117

-------------------------------------------------------------------------------
| Id  | Operation         | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |           |       |       |     3 (100)|          |
|*  1 |  TABLE ACCESS FULL| EMPLOYEES |     1 |    40 |     3   (0)| 00:00:01 |
-------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1 / EMPLOYEES@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
      DB_VERSION('11.2.0.4')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$1")
      FULL(@"SEL$1" "EMPLOYEES"@"SEL$1")
      END_OUTLINE_DATA
  */

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

   1 - filter("EMPLOYEE_ID"=100)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "EMPLOYEE_ID"[NUMBER,22], "LAST_NAME"[VARCHAR2,25],
       "SALARY"[NUMBER,22]

Note
-----
   - dynamic sampling used for this statement (level=2)


47 rows selected.


當然也可以不指定child_cursor_number,獲取所有版本的執行計劃。
SQL> select * from table(dbms_xplan.display_cursor('ftsw1cm8qh6nq',null,'advanced'));

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
SQL_ID  ftsw1cm8qh6nq, child number 0
-------------------------------------
select employee_id,last_name,salary from employees where employee_id=100

Plan hash value: 1833546154

---------------------------------------------------------------------------------------------
| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |               |       |       |     1 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMPLOYEES     |     1 |    16 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | EMP_EMP_ID_PK |     1 |       |     0   (0)|          |
---------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1 / EMPLOYEES@SEL$1
   2 - SEL$1 / EMPLOYEES@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
      DB_VERSION('11.2.0.4')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$1")
      INDEX_RS_ASC(@"SEL$1" "EMPLOYEES"@"SEL$1" ("EMPLOYEES"."EMPLOYEE_ID"))
      END_OUTLINE_DATA
  */

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

   2 - access("EMPLOYEE_ID"=100)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "EMPLOYEE_ID"[NUMBER,22], "LAST_NAME"[VARCHAR2,25], "SALARY"[NUMBER,22]
   2 - "EMPLOYEES".ROWID[ROWID,10], "EMPLOYEE_ID"[NUMBER,22]

SQL_ID  ftsw1cm8qh6nq, child number 1
-------------------------------------
select employee_id,last_name,salary from employees where employee_id=100

Plan hash value: 1445457117

-------------------------------------------------------------------------------
| Id  | Operation         | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |           |       |       |     3 (100)|          |
|*  1 |  TABLE ACCESS FULL| EMPLOYEES |     1 |    40 |     3   (0)| 00:00:01 |
-------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$1 / EMPLOYEES@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
      DB_VERSION('11.2.0.4')
      ALL_ROWS
      OUTLINE_LEAF(@"SEL$1")
      FULL(@"SEL$1" "EMPLOYEES"@"SEL$1")
      END_OUTLINE_DATA
  */

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

   1 - filter("EMPLOYEE_ID"=100)

Column Projection Information (identified by operation id):
-----------------------------------------------------------

   1 - "EMPLOYEE_ID"[NUMBER,22], "LAST_NAME"[VARCHAR2,25],
       "SALARY"[NUMBER,22]

Note
-----
   - dynamic sampling used for this statement (level=2)


92 rows selected.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2139242/,如需轉載,請註明出處,否則將追究法律責任。

相關文章