5.激活跟踪功能提供执行计划。
1.EXPLAIN PLAN 语句
EXPLAIN PLAN
[ SET STATEMENT_ID = string ]
[ INTO [ schema. ] table [ @ dblink ] ]
FOR statement ;
SET STATEMENT_ID Clause:
Specify a value for the STATEMENT_ID column for the rows of the execution plan in the output table. You can then use this value to identify these rows among others in the output table. Be sure to specify a STATEMENT_ID value if your output table contains rows from many execution plans. If you omit this clause, then the STATEMENT_ID value defaults to null.
INTO table Clause:
Specify the name of the output table, and optionally its schema and database. This table must exist before you use the EXPLAIN PLAN statement. --如果不存在执行$ORACLE_HOME/rdbms/admin/utlxplan.sql
If you omit schema, then the database assumes the table is in your own schema.
The dblink can be a complete or partial name of a database link to a remote Oracle Database where the output table is located. You can specify a remote output table only if you are using Oracle Database distributed functionality. If you omit dblink, then the database assumes the table is on your local database. See "References to Objects in Remote Databases" for information on referring to database links.
If you omit INTO altogether, then the database assumes an output table named PLAN_TABLE in your own schema on your local database.
FOR statement Clause:
Specify a SELECT, INSERT, UPDATE, DELETE, MERGE, CREATE TABLE, CREATE INDEX, or ALTER INDEX ... REBUILD statement for which the execution plan is generated.
--实验
SYS@PROD1> explain plan for select * from t;
Explained.
SYS@PROD1> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 8000 | 138 (1)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T | 2000 | 8000 | 138 (1)| 00:00:01 |
--------------------------------------------------------------------------
8 rows selected.
--指定参数
SYS@PROD1> explain plan set statement_id='tt' into plan_table for select * from t;
Explained.
SYS@PROD1> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 8000 | 138 (1)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T | 2000 | 8000 | 138 (1)| 00:00:01 |
--------------------------------------------------------------------------
8 rows selected.
SYS@PROD1> select statement_id,plan_id,operation,object_name,cost,cpu_cost from plan_table;
STATEMENT_ PLAN_ID OPERATION OBJECT_NAM COST CPU_COST
---------- ---------- ------------------------------ ---------- ---------- ----------
302 SELECT STATEMENT 138 7421440
302 TABLE ACCESS T 138 7421440
tt 303 SELECT STATEMENT 138 7421440
tt 303 TABLE ACCESS T 138 7421440
2.动态性能视图
相关视图:v$sql_plan
v$sql_plan_statistics
v$sql_workarea
v$sql_plan_statistics_all
(1).获取当前连接到实例的有关SQL信息
--此时另一session的EODA用户正在执行select count(*) from big_table;
SYS@PROD1> select sql_id,sql_child_number from v$session where username='EODA';
SQL_ID SQL_CHILD_NUMBER
------------- ----------------
6fxg0qj7j7ftd 0
(2).仅通过SQL语句内容就可获得有关SQL信息
SYS@PROD1> select sql_id,child_number,sql_text from v$sql where sql_fulltext like '%big_table%' and sql_text not like '%v$sql%';
SQL_ID CHILD_NUMBER SQL_TEXT
------------- ------------ --------------------------------------------------
6fxg0qj7j7ftd 0 select count(*) from big_table
SYS@PROD1> select * from table(dbms_xplan.display_cursor('6fxg0qj7j7ftd',0));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 6fxg0qj7j7ftd, child number 0
-------------------------------------
select count(*) from big_table
Plan hash value: 1764098166
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 3213 (100)| |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| BIG_TABLE_PK | 9980K| 3213 (4)| 00:00:01 |
------------------------------------------------------------------------------
14 rows selected.
3.AWR
捕获某个快照时,AWR能够收集执行计划,其中都保存在dba_hist_sql_plan中。
--此处实验紧跟在上述的动态性能视图实验后
SYS@PROD1> select count(*) from dba_hist_sql_plan;
COUNT(*)
----------
10450
SYS@PROD1> select * from table(dbms_xplan.display_awr('6fxg0qj7j7ftd')); --无结果
no rows selected
手动生成一个新的AWR报表:
SYS@PROD1> exec dbms_workload_repository.create_snapshot;
SYS@PROD1> start /u01/app/oracle/product/11.2.0/dbhome_1/rdbms/admin/awrrpt.sql
End of Report