如果某条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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28536251/viewspace-2139242/