[20120112]謹慎使用set autotrace traceonly檢視執行計劃.txt
自己經常在最佳化sql語句是經常使用set autotrace traceonly來最佳化sql語句.這樣的好處是如果輸出記錄很多,可以不受干擾,直接檢視執行計劃以及執行計劃的統計資訊.但是在一些特殊情況要注意也許執行計劃是不真實的.
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
1.建立測試表:
SQL> create table t2 (id number, name varchar2(100)) ;
Table created.
SQL> create index i_t2_id on t2(id);
Index created.
2.測試1:
SQL> insert into t2 select rownum id ,'test2' from dual connect by level <=10000;
10000 rows created.
SQL> commit ;
Commit complete.
SQL> set autot traceonly ;
SQL> select * from t2 where id=45;
Execution Plan
----------------------------------------------------------
Plan hash value: 3661687773
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 65 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 65 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_T2_ID | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
596 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autot off
SQL> select * from t2 where id=45;
SQL> select * from table(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
--------------------------------------
SQL_ID 03dqfy40b7t8r, child number 0
-------------------------------------
select * from t2 where id=45
Plan hash value: 3661687773
--------------------------------------------------------
| Id | Operation | Name | E-Rows |
--------------------------------------------------------
| 0 | SELECT STATEMENT | | |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 |
|* 2 | INDEX RANGE SCAN | I_T2_ID | 1 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
--我沒有分析所以,採用的是dynamic sampling.
3.測試2:
SQL> delete from t2;
10000 rows deleted.
SQL> insert into t2 select 45 ,'test2' from dual connect by level <=10000;
10000 rows created.
SQL> commit ;
SQL> set autot traceonly ;
SQL> select * from t2 where id=45;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 634K| 8 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T2 | 10000 | 634K| 8 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
183 consistent gets
0 physical reads
2224 redo size
149746 bytes sent via SQL*Net to client
1058 bytes received via SQL*Net from client
51 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
--注意使用set autot traceonly 看到的是全表掃描.
SQL> set autot off
SQL> select * from t2 where id=45;
SQL> select * from table(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 03dqfy40b7t8r, child number 0
-------------------------------------
select * from t2 where id=45
Plan hash value: 3661687773
--------------------------------------------------------
| Id | Operation | Name | E-Rows |
--------------------------------------------------------
| 0 | SELECT STATEMENT | | |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 |
|* 2 | INDEX RANGE SCAN | I_T2_ID | 1 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system level
26 rows selected.
實際上真正的執行計劃是INDEX RANGE SCAN!
當然這個僅僅是特例!!像使用explain plan for和dbms_xplan.display也是一樣的問題,自己在工作要注意這些細節.
SQL> explain plan for select * from t2 where id=45;
Explained.
SQL> select * from table(dbms_xplan.display(NULL,NULL,'ALLSTATS LAST PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
------------------------------------------
Plan hash value: 1513984157
-------------------------------------------
| Id | Operation | Name | E-Rows |
-------------------------------------------
| 0 | SELECT STATEMENT | | 10000 |
|* 1 | TABLE ACCESS FULL| T2 | 10000 |
-------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system level
20 rows selected.
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
1.建立測試表:
SQL> create table t2 (id number, name varchar2(100)) ;
Table created.
SQL> create index i_t2_id on t2(id);
Index created.
2.測試1:
SQL> insert into t2 select rownum id ,'test2' from dual connect by level <=10000;
10000 rows created.
SQL> commit ;
Commit complete.
SQL> set autot traceonly ;
SQL> select * from t2 where id=45;
Execution Plan
----------------------------------------------------------
Plan hash value: 3661687773
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 65 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 65 | 1 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_T2_ID | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
596 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autot off
SQL> select * from t2 where id=45;
SQL> select * from table(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
--------------------------------------
SQL_ID 03dqfy40b7t8r, child number 0
-------------------------------------
select * from t2 where id=45
Plan hash value: 3661687773
--------------------------------------------------------
| Id | Operation | Name | E-Rows |
--------------------------------------------------------
| 0 | SELECT STATEMENT | | |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 |
|* 2 | INDEX RANGE SCAN | I_T2_ID | 1 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
--我沒有分析所以,採用的是dynamic sampling.
3.測試2:
SQL> delete from t2;
10000 rows deleted.
SQL> insert into t2 select 45 ,'test2' from dual connect by level <=10000;
10000 rows created.
SQL> commit ;
SQL> set autot traceonly ;
SQL> select * from t2 where id=45;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10000 | 634K| 8 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T2 | 10000 | 634K| 8 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
183 consistent gets
0 physical reads
2224 redo size
149746 bytes sent via SQL*Net to client
1058 bytes received via SQL*Net from client
51 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
10000 rows processed
--注意使用set autot traceonly 看到的是全表掃描.
SQL> set autot off
SQL> select * from t2 where id=45;
SQL> select * from table(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID 03dqfy40b7t8r, child number 0
-------------------------------------
select * from t2 where id=45
Plan hash value: 3661687773
--------------------------------------------------------
| Id | Operation | Name | E-Rows |
--------------------------------------------------------
| 0 | SELECT STATEMENT | | |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 |
|* 2 | INDEX RANGE SCAN | I_T2_ID | 1 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system level
26 rows selected.
實際上真正的執行計劃是INDEX RANGE SCAN!
當然這個僅僅是特例!!像使用explain plan for和dbms_xplan.display也是一樣的問題,自己在工作要注意這些細節.
SQL> explain plan for select * from t2 where id=45;
Explained.
SQL> select * from table(dbms_xplan.display(NULL,NULL,'ALLSTATS LAST PEEKED_BINDS'));
PLAN_TABLE_OUTPUT
------------------------------------------
Plan hash value: 1513984157
-------------------------------------------
| Id | Operation | Name | E-Rows |
-------------------------------------------
| 0 | SELECT STATEMENT | | 10000 |
|* 1 | TABLE ACCESS FULL| T2 | 10000 |
-------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=45)
Note
-----
- dynamic sampling used for this statement (level=2)
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system level
20 rows selected.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-714782/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20121212]謹慎使用set autotrace traceonly檢視執行計劃[補充].txt
- 檢視sql執行計劃--set autotraceSQL
- 使用set autotrace on 檢視資料庫執行計劃資料庫
- 檢視sql執行計劃--set autotrace [final]SQL
- 開啟執行計劃set autotrace on
- 【顯示執行計劃】在普通使用者下使用set autot 檢視執行計劃
- 以autotrace檢視執行計劃時換行的解決
- Oracle 9i 開啟autotrace on 檢視執行計劃Oracle
- set autotrace on 產生不準確的執行計劃
- 檢視執行計劃
- 使用PL/SQL檢視執行計劃SQL
- [20120209] SET AUTOTRACE TRACEONLY EXPLAIN的問題.txtAI
- 關於檢視Oracle資料庫執行計劃的命令(set autotrace的用法和含意及區別[轉])Oracle資料庫
- 使用EXPLAIN PLAN來檢視執行計劃AI
- 檢視執行計劃(一)
- 檢視執行計劃(二)
- 檢視sql執行計劃SQL
- 獲取執行計劃之Autotrace
- Oracle檢視執行計劃(五)Oracle
- Oracle檢視執行計劃(六)Oracle
- Oracle檢視執行計劃(一)Oracle
- Oracle檢視執行計劃(二)Oracle
- Oracle檢視執行計劃(三)Oracle
- Oracle檢視執行計劃(四)Oracle
- 檢視歷史執行計劃
- ORACLE執行計劃的檢視Oracle
- oracle如何檢視執行計劃Oracle
- 檢視oracle執行計劃 - 轉Oracle
- 檢視執行計劃的方法
- 檢視 OceanBase 執行計劃
- sql中使用函式導致explain plan for和set autotrace得到執行計劃不準確SQL函式AI
- 以scott使用者執行set autotrace 出錯
- 使用10046事件檢視oracle執行計劃事件Oracle
- 如何檢視SQL的執行計劃SQL
- Oracle檢視執行計劃的命令Oracle
- DBMS_XPLAN檢視執行計劃
- 檢視SQL的執行計劃方法SQL
- oracle檢視執行計劃的方法Oracle