sql執行計劃:把SQL語句拆分為每個的操作步驟組合,按照一定的順序執行得出結果,查看並看懂執行計劃是調優的關鍵步驟 查看執行計劃的方法 DBMS_XPLAN包 sql*plus AUTO trace V$SQL_PLAN、DBA_HIST_SQL_PLAN ?/rdbms/admin/awrsqr ...
sql執行計劃:把SQL語句拆分為每個的操作步驟組合,按照一定的順序執行得出結果,查看並看懂執行計劃是調優的關鍵步驟
查看執行計劃的方法
- DBMS_XPLAN包
- sql*plus AUTO trace
- V$SQL_PLAN、DBA_HIST_SQL_PLAN
- ?/rdbms/admin/awrsqrpt.sql
- 工具類:toad、pl/SQL DEV
- 跟蹤dump:10046,10053
大多數人比較喜歡用工具直接看,以下來說明這些方式的不同用處
一、DBMS_XPLAN包
10g以後可以查看AWR中的語句執行計劃,也可以查看當前記憶體中游標的執行計劃。
主要有以下五個方法:
-
DISPLAY
- 格式化並顯示plan table中內容,類似工具的F5(EXPLAIN PLAN FOR …) -
DISPLAY_AWR
-格式和顯示存儲在AWR中的SQL語句的執行計劃。數據來源: DBA_HIST_SQL_PLAN,DBA_HIST_SQLTEXT -
DISPLAY_CURSOR
-格式和顯示任何載入游標執行計劃的內容。數據來源:V$SQL_PLAN和V$SQL_PLAN_STATISTICS_ALL -
DISPLAY_SQL_PLAN_BASELINE
- 顯示一個或多個執行計劃的SQL語句通過SQL handle 標識 。數據來源:DBA_SQL_PLAN_BASELINES -
DISPLAY_SQLSET
- 格式和顯示存儲在sql調優集中的執行計劃內容。數據來源:ALL_SQLSET_STATEMENTS、ALL_SQLSET_PLANS
最常用的DISPLAY_AWR和DISPLAY_CURSOR,來闡述具體的調用方式,顯示的內容
(一)、DISPLAY_CURSOR
顯示當前shared pool->library cache中的執行計劃
DBMS_XPLAN.DISPLAY_CURSOR( sql_id IN VARCHAR2 DEFAULT NULL, cursor_child_no IN NUMBER DEFAULT 0, format IN VARCHAR2 DEFAULT 'TYPICAL');
參數 | 說明(常用說明) |
sql_id | SQL的唯一標識,取SELECT SQL_ID FROM V$SQL,為null的情況下,取上一條語句的執行計劃 |
cursor_child_no | 子游標的id號,為null,取出全部子游標的執行計劃 |
format | 輸出格式化 預設TYPICAL ADVANCED +PEEKED_BINDS ALLSTATS |
例子查看
#預設格式輸出
select * from table(dbms_xplan.display_cursor('bjqjt2dfvya84',null)) SQL_ID bjqjt2dfvya84, child number 0 ------------------------------------- select * from emp where empno=7521 Plan hash value: 2949544139 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 1 (100)| | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 38 | 1 (0)| 00:00:01 | |* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO"=7521) #格式化為ADVANCED +PEEKED_BINDS select * from table(dbms_xplan.display_cursor('bjqjt2dfvya84',null,'ADVANCED +PEEKED_BINDS')) SQL_ID bjqjt2dfvya84, child number 0 ------------------------------------- select * from emp where empno=7521 Plan hash value: 2949544139 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 1 (100)| | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 38 | 1 (0)| 00:00:01 | |* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| | -------------------------------------------------------------------------------------- Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------- 1 - SEL$1 / EMP@SEL$1 2 - SEL$1 / EMP@SEL$1 Outline Data ------------- /*+ BEGIN_OUTLINE_DATA IGNORE_OPTIM_EMBEDDED_HINTS OPTIMIZER_FEATURES_ENABLE('11.2.0.3') DB_VERSION('11.2.0.3') OPT_PARAM('optimizer_dynamic_sampling' 0) ALL_ROWS OUTLINE_LEAF(@"SEL$1") INDEX_RS_ASC(@"SEL$1" "EMP"@"SEL$1" ("EMP"."EMPNO")) END_OUTLINE_DATA */ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO"=7521) Column Projection Information (identified by operation id): ----------------------------------------------------------- 1 - "EMPNO"[NUMBER,22], "EMP"."ENAME"[VARCHAR2,10], "EMP"."JOB"[VARCHAR2,9], "EMP"."MGR"[NUMBER,22], "EMP"."HIREDATE"[DATE,7], "EMP"."SAL"[NUMBER,22], "EMP"."COMM"[NUMBER,22], "EMP"."DEPTNO"[NUMBER,22] 2 - "EMP".ROWID[ROWID,10], "EMPNO"[NUMBER,22]
#格式化ALLSTATS 兩種處理方式: * hint 'gather_plan_statistics' is used for the statement or * parameter 'statistics_level' is set to 'ALL', at session or system level select /*+gather_plan_statistics*/ * from emp where empno=7521 select * from table(dbms_xplan.display_cursor('dvj95t2z1gh2a',null,'ALLSTATS')) SQL_ID dvj95t2z1gh2a, child number 0 ------------------------------------- select /*+gather_plan_statistics*/ * from emp where empno=7521 Plan hash value: 2949544139 ------------------------------------------------------------------------------------------------ | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 2 | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 1 | 1 |00:00:00.01 | 2 | |* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | 1 | 1 |00:00:00.01 | 1 | ------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO"=7521)A-ROW是實際的行數,E-ROW是優化器評估的
(二)、DISPLAY_AWR
顯示AWR中的SQL的執行計劃,對分析歷史SQL慢的相當有幫組
DBMS_XPLAN.DISPLAY_AWR( sql_id IN VARCHAR2, plan_hash_value IN NUMBER DEFAULT NULL, db_id IN NUMBER DEFAULT NULL, format IN VARCHAR2 DEFAULT TYPICAL);
參數 | 常用說明 |
SQL_ID | SQL的唯一標識,取SELECT SQL_ID FROM DBA_HIST_SQLTEXT |
PLAN_HASH_VALUE | 指定SQL語句的PLAN_HASH_VALUE,忽略取全部的SQL_ID下的執行計劃 |
DB_ID | 預設取V$DATABASE.DATABASE_ID |
FORMAT | 輸出格式化 預設TYPICAL ADVANCED +PEEKED_BINDS |
例子查看
#預設輸出的格式 select * from table(dbms_xplan.display_awr('bjqjt2dfvya84')); SQL_ID bjqjt2dfvya84 -------------------- select * from emp where empno=7521 Plan hash value: 2949544139 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 1 (100)| | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 38 | 1 (0)| 00:00:01 | | 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| | -------------------------------------------------------------------------------------- #通過格式化ADVANCED +PEEKED_BINDS select * from table(dbms_xplan.display_awr('bjqjt2dfvya84',FORMAT=>'ADVANCED +PEEKED_BINDS')); SQL_ID bjqjt2dfvya84 -------------------- select * from emp where empno=7521 Plan hash value: 2949544139 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 1 (100)| | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 38 | 1 (0)| 00:00:01 | | 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| | -------------------------------------------------------------------------------------- Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------- 1 - SEL$1 / EMP@SEL$1 2 - SEL$1 / EMP@SEL$1 Outline Data ------------- /*+ BEGIN_OUTLINE_DATA IGNORE_OPTIM_EMBEDDED_HINTS OPTIMIZER_FEATURES_ENABLE('11.2.0.3') DB_VERSION('11.2.0.3') OPT_PARAM('optimizer_dynamic_sampling' 0) ALL_ROWS OUTLINE_LEAF(@"SEL$1") INDEX_RS_ASC(@"SEL$1" "EMP"@"SEL$1" ("EMP"."EMPNO")) END_OUTLINE_DATA */
二、sqlplus autotrace
scott@GULL> set autotrace
用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
可以體現執行語句的執行計劃和統計信息
scott@GULL> SET autotrace trace scott@GULL> select * from emp where empno=7521; 執行計劃 ---------------------------------------------------------- Plan hash value: 2949544139 -------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 38 | 1 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 38 | 1 (0)| 00:00:01 | |* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)| 00:00:01 | -------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("EMPNO"=7521) 統計信息 ---------------------------------------------------------- 0 recursive calls 0 db block gets 2 consistent gets 0 physical reads 0 redo size 902 bytes sent via SQL*Net to client 508 bytes received via SQL*Net from client 1 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed scott@GULL>
三、V$SQL_PLAN
直接取數據字典的源數據
scott@GULL> col id format 99; scott@GULL> col parent_id format 99999; scott@GULL> col operation format a25; scott@GULL> col options format a20; scott@GULL> select id,parent_id,operation,options from V$SQL_PLAN where sql_id='bjqjt2dfvya84'; ID PARENT_ID OPERATION OPTIONS --- --------- ------------------------- -------------------- 0 SELECT STATEMENT 1 0 TABLE ACCESS BY INDEX ROWID 2 1 INDEX UNIQUE SCAN
四、?/rdbms/admin/awrsqrpt.sql
查看AWR中執行計劃,調用這個腳本後,相關的直觀展現
scott@GULL> @?/rdbms/admin/awrsqrpt.sql Current Instance ~~~~~~~~~~~~~~~~ DB Id DB Name Inst Num Instance ----------- ------------ -------- ------------ 3239200222 GULL 1 gullstby Specify the Report Type ~~~~~~~~~~~~~~~~~~~~~~~ Would you like an HTML report, or a plain text report? Enter 'html' for an HTML report, or 'text' for plain text Defaults to 'html' 輸入 report_type 的值: html Type Specified: html Instances in this Workload Repository schema ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DB Id Inst Num DB Name Instance Host ------------ -------- ------------ ------------ ------------ * 3239200222 1 GULL gullstby gull02 Using 3239200222 for database Id Using 1 for instance number Specify the number of days of snapshots to choose from ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Entering the number of days (n) will result in the most recent (n) days of snapshots being listed. Pressing <return> without specifying a number lists all completed snapshots. 輸入 num_days 的值: 1 Listing the last day's Completed Snapshots Snap Instance DB Name Snap Id Snap Started Level ------------ ------------ --------- ------------------ ----- gullstby GULL 368 01 6月 2016 10:08 1 369 01 6月 2016 10:12 1 Specify the Begin and End Snapshot Ids ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 輸入 begin_snap 的值: 368 Begin Snapshot Id specified: 368 輸入 end_snap 的值: 369 End Snapshot Id specified: 369 Specify the SQL Id ~~~~~~~~~~~~~~~~~~ 輸入 sql_id 的值: bjqjt2dfvya84 SQL ID specified: bjqjt2dfvya84 Specify the Report Name ~~~~~~~~~~~~~~~~~~~~~~~ The default report file name is awrsqlrpt_1_368_369.html. To use this name, press <return> to continue, otherwise enter an alternative. 輸入 report_name 的值: /home/oracle/0601.html
五、工具類:toad(ctrl+e)、pl/SQL DEV(f5)
工具類查看執行計劃,非常快捷、簡單、能清楚知道每部步驟的優先順序,但是他們實際調用的是EXPLAIN PLAN for,寫入plan_TABLE,是優化器的預評估,不是正在SQL執行時的執行計劃。簡單分析是有用的,但是在你的應用程式中SQL很慢,常用的SQL用toad等很快,此時就不要在用工具快捷方式去查看執行計劃,要去分析正在執行的執行計劃,採用以上的內容方式。
toad(ctrl+e)
五、10046
10046這個事件是查看SQL具體的執行步驟,等待的是什麼事件,實際內部調用的是哪些SQL。SQL語句執行很慢時,可以查看具體等待的是什麼,導致執行這麼慢。還有對我們瞭解create 、truncate具體在執行哪些語句有幫組。
SQL> conn /as sysdba 已連接。 SQL> ALTER SESSION SET EVENTS '10046 trace name context forever, level 28'; 會話已更改。 SQL> select * from scott.emp where empno=7521; SQL> select c.value || '/' || d.instance_name || '_ora_' || 2 a.spid || '.trc' || 3 case when e.value is not null then '_'||e.value end trace 4 from v$process a, v$session b, v$parameter c, v$instance d, v$parameter e 5