一、 利用autotrace查看執行計劃
註意:autotrace所查詢的執行計劃並不是真實的執行計劃(這個計劃是從PLAN_TABLE中來的),是CBO預估的
SQL> set autot
用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
方括號內的字母都可以省略。
set autot on ----執行SQL 並且顯示執行計劃和統計信息
set autot trace ----執行SQL 但不顯示運行結果,顯示執行計劃和統計信息
set autot trace exp ----如果SELECT 就不執行SQL(dml 執行),只顯示執行計劃
set autot trace stat ----執行SQL,只顯示統計信息
註意:使用autotrace去看執行計劃,最關心的是物理讀,邏輯讀和返回的行數。
舉例說明:
SQL> set autot trace;
SQL> set line 200;
SQL> set pages 200;
SQL> select * from test where owner='SYS';
已選擇109048行。
執行計劃
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 18163 | 1755K| 1375 (1)| 00:00:17 |
|* 1 | TABLE ACCESS FULL| TEST | 18163 | 1755K| 1375 (1)| 00:00:17 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SYS')
統計信息
----------------------------------------------------------
40 recursive calls
0 db block gets
12245 consistent gets
0 physical reads
0 redo size
4983040 bytes sent via SQL*Net to client
80430 bytes received via SQL*Net from client
7271 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
109048 rows processed
發現相差很大(自我判斷和實際返回的行數)於是進行統計信息的收集,再次進行分析執行計劃。
SQL> select * from test where owner='SYS';
已選擇109048行。
執行計劃
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 109K| 10M| 1376 (1)| 00:00:17 |
|* 1 | TABLE ACCESS FULL| TEST | 109K| 10M| 1376 (1)| 00:00:17 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SYS')
統計信息
----------------------------------------------------------
1 recursive calls
0 db block gets
12175 consistent gets
0 physical reads
0 redo size
4983040 bytes sent via SQL*Net to client
80430 bytes received via SQL*Net from client
7271 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
109048 rows processed
二、註意:EXPLAIN PLAN FOR ......所查詢的執行計劃並不是真實的執行計劃,是CBO預估的。
首先 EXPLAIN PLAN FOR SQL_TEXT;
顯示普通執行計劃 SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
顯示高級執行計劃 SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(FORMAT=>'ADVANCED -PROJECTION'));
SQL> explain plan for select * from test where owner='SYS';
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 109K| 10M| 1376 (1)| 00:00:17 |
|* 1 | TABLE ACCESS FULL| TEST | 109K| 10M| 1376 (1)| 00:00:17 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SYS')
已選擇13行。
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(FORMAT=>'ADVANCED -PROJECTION'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 109K| 10M| 1376 (1)| 00:00:17 |
|* 1 | TABLE ACCESS FULL| TEST | 109K| 10M| 1376 (1)| 00:00:17 |
--------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / TEST@SEL$1
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
FULL(@"SEL$1" "TEST"@"SEL$1")
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
DB_version('11.2.0.4')
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
IGNORE_OPTIM_EMBEDDED_HINTS
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SYS')
已選擇32行。
SQL> explain plan for select ename,deptno from emp where deptno in (select /*+ no_unnest */ deptno from dept where dname='CHICAGO');
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(FORMAT=>'ADVANCED -PROJECTION'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2809975276
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 45 | 6 (0)| 00:00:01 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL | EMP | 14 | 126 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 13 | 1 (0)| 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
2 - SEL$1 / EMP@SEL$1
3 - SEL$2 / DEPT@SEL$2
4 - SEL$2 / DEPT@SEL$2
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
INDEX_RS_ASC(@"SEL$2" "DEPT"@"SEL$2" ("DEPT"."DEPTNO"))
FULL(@"SEL$1" "EMP"@"SEL$1")
OUTLINE_LEAF(@"SEL$1")
OUTLINE_LEAF(@"SEL$2")
ALL_ROWS
DB_VERSION('11.2.0.4')
OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
IGNORE_OPTIM_EMBEDDED_HINTS
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter( EXISTS (SELECT /*+ NO_UNNEST */ 0 FROM "DEPT" "DEPT" WHERE
"DEPTNO"=:B1 AND "DNAME"='CHICAGO'))
3 - filter("DNAME"='CHICAGO')
4 - access("DEPTNO"=:B1)
已選擇43行。
三、特殊執行計劃(真正的執行計劃)
做這個 一定要關閉 autotrace
ALTER SESSION SET STATISTICS_LEVEL=ALL; ---再運行SQL
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
例子:
create table test as select * from dba_objects;
create index test_owner_indx on test (owner);
SQL> alter session set statistics_level=all;
SQL> select count(*) from test where owner='SCOTT';
COUNT(*)
----------
12
SQL> select count(*) from test where owner='SCOTT';
COUNT(*)
----------
12
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID cj55b1jx5mqxt, child number 0
-------------------------------------
select count(*) from test where owner='SCOTT'
Plan hash value: 3066878202
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 2 |
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.01 | 2 |
|* 2 | INDEX RANGE SCAN| TEST_OWNER_INDX | 1 | 12 | 12 |00:00:00.01 | 2 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='SCOTT')
Note
-----
- dynamic sampling used for this statement (level=2)
已選擇23行。
Starts表示這個操作執行了幾次
E-Rows表示優化器估算的行數
A-Rows 表示實際的行數
A-Time 表示這個操作執行的時間(累加的)
Buffers 表示邏輯度(累加的)
方法四、真正的執行計劃
查看v$sql_plan表
通過SQL語句的SQL_ID和子遊標號,可以在V$SQL_PLAN表中查看到該SQL語句的執行計劃。
例如:執行了語句“select * from t1;',先通過v$sqlarea和v$sql找到該語句的SQL_ID和子遊標號。
SQL> select sql_id,sql_text from v$sqlarea where sql_text like '%from t1%';
SQL_ID SQL_TEXT
------------- ----------------------------------------------------------------------------------------------------
27uhu2q2xuu7r select * from t1
bf45pybkumcx5 select sql_id,sql_text from v$sqlarea where sql_text like '%from t1%'
SQL> select sql_id,child_number,sql_text from v$sql where sql_id='27uhu2q2xuu7r';
SQL_ID CHILD_NUMBER SQL_TEXT
------------- ------------ ----------------------------------------------------------------------------------------------------
27uhu2q2xuu7r 0 select * from t1
--通過以上兩條查詢語句,查得目標SQL語句的SQL_ID為 “27uhu2q2xuu7r”, 子遊標號為“0”.
SQL> select timestamp,operation,options,object_name,cost,id,parent_id from v$sql_plan where sql_id='27uhu2q2xuu7r' and child_number=0;
TIMESTAMP OPERATION OPTIONS OBJECT_NAME COST IDPARENT_ID
----------------- -------------------- ---------- ------------------------------ ---------- ---------- ----------
20131221 20:49:14 SELECT STATEMENT 3 0
20131221 20:49:14 TABLE ACCESS FULL T1 3 1 0
(二)、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
例子查看
anit
#默認輸出的格式
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
*/
Tags:
文章來源: