【Explain Plan】檢視SQL的執行計劃
1.【使用方法】
Explain Plan在sqlplus中使用起來非常的便捷,使用方法如下
sec@ora10g> explain plan for select count(*) from t;
Explained.
sec@ora10g> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
Plan hash value: 2966233522
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8407 (1)| 00:01:41 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 3017K| 8407 (1)| 00:01:41 |
-------------------------------------------------------------------
9 rows selected.
2.【擴充套件補充】
在10g以前的版本中,需要單獨建立PLAN_TABLE並授予,10g中自動建立PLAN_TABLE$不再需要這一步驟
下面是10g之前初始化PLAN_TABLE需要建立的步驟:
SQL> @?/rdbms/admin/utlxplan.sql
SQL> create public synonym plan_table for plan_table;
SQL> grant all on plan_table to public;
進一步談一下有關怎麼樣確定Oracle 10g中用到的字典表是PLAN_TABLE$而不再是PLAN_TABLE
1)確定資料庫版本
sec@ora10g> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
2)開啟autotrace功能
sec@ora10g> set autotrace on explain;
3)跟蹤後在執行計劃中得到PLAN_TABLE$的資訊
sec@ora10g> select * from plan_table;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 103984305
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 11081 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| PLAN_TABLE$ | 1 | 11081 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------
3.小結 and “意猶未盡”
獲得SQL的執行計劃是對SQL進行最佳化的第一步,然而使用“explain plan for”方法又是一個最基本的獲得執行計劃的手段,當然,您也可以選擇使用autotrace和sql_trace等工具獲得SQL的執行計劃。
在使用explain plan for方法得到SQL執行計劃的過程中,要頻繁的使用“select * from table(dbms_xplan.display());”語句檢視執行計劃,有些人感覺這樣操作非常的不便捷,而且如果不是經常使用就會忘記具體的語法格式,這裡最後給出一個迅速檢視的另外一種方法——使用Oracle的非常貼心的utlxplp.sql指令碼進行檢視。
在“$ORACLE_HOME/rdbms/admin”目錄下存有一個名叫utlxplp.sql的SQL指令碼,其中非常人性化的記錄了這個SQL的語法格式,您也可以使用這個指令碼直接檢視執行計劃。
使用方法如下:
sec@ora10g> explain plan for select count(*) from t;
Explained.
sec@ora10g> @?/rdbms/admin/utlxplp.sql
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------
Plan hash value: 2966233522
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 12779 | 37 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
13 rows selected.
最後,摘錄一下這個指令碼的原貌,供參考
$ cat $ORACLE_HOME/rdbms/admin/utlxplp.sql
Rem
Rem $Header: utlxplp.sql 23-jan-2002.08:55:23 bdagevil Exp $
Rem
Rem utlxplp.sql
Rem
Rem Copyright (c) 1998, 2002, Oracle Corporation. All rights reserved.
Rem
Rem NAME
Rem utlxplp.sql - UTiLity eXPLain Parallel plans
Rem
Rem DESCRIPTION
Rem script. utility to display the explain plan of the last explain plan
Rem command. Display also Parallel Query information if the plan happens to
Rem run parallel
Rem
Rem NOTES
Rem Assume that the table PLAN_TABLE has been created. The script
Rem utlxplan.sql should be used to create that table
Rem
Rem With SQL*plus, it is recomended to set linesize and pagesize before
Rem running this script. For example:
Rem set linesize 130
Rem set pagesize 0
Rem
Rem MODIFIED (MM/DD/YY)
Rem bdagevil 01/23/02 - rewrite with new dbms_xplan package
Rem bdagevil 04/05/01 - include CPU cost
Rem bdagevil 02/27/01 - increase Name column
Rem jihuang 06/14/00 - change order by to order siblings by.
Rem jihuang 05/10/00 - include plan info for recursive SQL in LE row source
Rem bdagevil 01/05/00 - make deterministic with order-by
Rem bdagevil 05/07/98 - Explain plan script. for parallel plans
Rem bdagevil 05/07/98 - Created
Rem
set markup html preformat on
Rem
Rem Use the display table function from the dbms_xplan package to display the last
Rem explain plan. Use default mode which will display only relevant information
Rem
select * from table(dbms_xplan.display());
-- The End --
Explain Plan在sqlplus中使用起來非常的便捷,使用方法如下
sec@ora10g> explain plan for select count(*) from t;
Explained.
sec@ora10g> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
Plan hash value: 2966233522
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8407 (1)| 00:01:41 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 3017K| 8407 (1)| 00:01:41 |
-------------------------------------------------------------------
9 rows selected.
2.【擴充套件補充】
在10g以前的版本中,需要單獨建立PLAN_TABLE並授予,10g中自動建立PLAN_TABLE$不再需要這一步驟
下面是10g之前初始化PLAN_TABLE需要建立的步驟:
SQL> @?/rdbms/admin/utlxplan.sql
SQL> create public synonym plan_table for plan_table;
SQL> grant all on plan_table to public;
進一步談一下有關怎麼樣確定Oracle 10g中用到的字典表是PLAN_TABLE$而不再是PLAN_TABLE
1)確定資料庫版本
sec@ora10g> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
2)開啟autotrace功能
sec@ora10g> set autotrace on explain;
3)跟蹤後在執行計劃中得到PLAN_TABLE$的資訊
sec@ora10g> select * from plan_table;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 103984305
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 11081 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| PLAN_TABLE$ | 1 | 11081 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------
3.小結 and “意猶未盡”
獲得SQL的執行計劃是對SQL進行最佳化的第一步,然而使用“explain plan for”方法又是一個最基本的獲得執行計劃的手段,當然,您也可以選擇使用autotrace和sql_trace等工具獲得SQL的執行計劃。
在使用explain plan for方法得到SQL執行計劃的過程中,要頻繁的使用“select * from table(dbms_xplan.display());”語句檢視執行計劃,有些人感覺這樣操作非常的不便捷,而且如果不是經常使用就會忘記具體的語法格式,這裡最後給出一個迅速檢視的另外一種方法——使用Oracle的非常貼心的utlxplp.sql指令碼進行檢視。
在“$ORACLE_HOME/rdbms/admin”目錄下存有一個名叫utlxplp.sql的SQL指令碼,其中非常人性化的記錄了這個SQL的語法格式,您也可以使用這個指令碼直接檢視執行計劃。
使用方法如下:
sec@ora10g> explain plan for select count(*) from t;
Explained.
sec@ora10g> @?/rdbms/admin/utlxplp.sql
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------
Plan hash value: 2966233522
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 12779 | 37 (0)| 00:00:01 |
-------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
13 rows selected.
最後,摘錄一下這個指令碼的原貌,供參考
$ cat $ORACLE_HOME/rdbms/admin/utlxplp.sql
Rem
Rem $Header: utlxplp.sql 23-jan-2002.08:55:23 bdagevil Exp $
Rem
Rem utlxplp.sql
Rem
Rem Copyright (c) 1998, 2002, Oracle Corporation. All rights reserved.
Rem
Rem NAME
Rem utlxplp.sql - UTiLity eXPLain Parallel plans
Rem
Rem DESCRIPTION
Rem script. utility to display the explain plan of the last explain plan
Rem command. Display also Parallel Query information if the plan happens to
Rem run parallel
Rem
Rem NOTES
Rem Assume that the table PLAN_TABLE has been created. The script
Rem utlxplan.sql should be used to create that table
Rem
Rem With SQL*plus, it is recomended to set linesize and pagesize before
Rem running this script. For example:
Rem set linesize 130
Rem set pagesize 0
Rem
Rem MODIFIED (MM/DD/YY)
Rem bdagevil 01/23/02 - rewrite with new dbms_xplan package
Rem bdagevil 04/05/01 - include CPU cost
Rem bdagevil 02/27/01 - increase Name column
Rem jihuang 06/14/00 - change order by to order siblings by.
Rem jihuang 05/10/00 - include plan info for recursive SQL in LE row source
Rem bdagevil 01/05/00 - make deterministic with order-by
Rem bdagevil 05/07/98 - Explain plan script. for parallel plans
Rem bdagevil 05/07/98 - Created
Rem
set markup html preformat on
Rem
Rem Use the display table function from the dbms_xplan package to display the last
Rem explain plan. Use default mode which will display only relevant information
Rem
select * from table(dbms_xplan.display());
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-616086/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用EXPLAIN PLAN來檢視執行計劃AI
- TOAD中檢視執行計劃(Explain Plan)AI
- 檢視執行計劃方法總結之一:explain plan命令AI
- Oracle執行計劃Explain Plan 如何使用OracleAI
- Oracle 執行計劃(Explain Plan) 說明OracleAI
- Oracle 執行計劃(Explain Plan) 說明OracleAI
- 【SQL_PLAN】Oracle 透過檢視sql_plan 格式化執行計劃SQLOracle
- 【檢視】使用V$SQL_PLAN檢視獲取曾經執行過的SQL語句執行計劃SQL
- 配置oracle 解釋執行計劃--explain planOracleAI
- 檢視sql執行計劃SQL
- 檢視一個正在執行的sql的執行計劃(explain for connection processlist_id)SQLAI
- 使用 EXPLAIN PLAN 獲取SQL語句執行計劃 (R0.1)AISQL
- 【最佳化】explain plan for 方式存取執行計劃AI
- 如何檢視SQL的執行計劃SQL
- 檢視SQL的執行計劃方法SQL
- Oracle 檢視SQL的執行計劃OracleSQL
- 【執行計劃】格式化EXPLAIN PLAN的輸出結果AI
- MySQL EXPLAIN命令詳解學習(檢視執行計劃)MySqlAI
- 使用PL/SQL檢視執行計劃SQL
- 檢視sql執行計劃--set autotraceSQL
- autotrace 和explain plan for可能導致執行計劃錯誤AI
- oracle explain plan for獲取執行計劃並不可靠.OracleAI
- Oracle優化——如何檢視語句的準確的執行計劃(explain plan可能不是真實的)Oracle優化AI
- MySQL——通過EXPLAIN分析SQL的執行計劃MySqlAI
- Oracle檢視正在執行的SQL以及執行計劃分析OracleSQL
- 檢視sql執行計劃方法彙總SQL
- MySQL 5.7 檢視理解SQL執行計劃MySql
- 檢視執行計劃
- 檢視sql 執行計劃的歷史變更SQL
- explain執行計劃分析AI
- mysql執行計劃explainMySqlAI
- mysql explain 執行計劃MySqlAI
- 根據SQL_ID檢視執行計劃SQL
- 多種方法檢視Oracle SQL執行計劃OracleSQL
- 檢視sql執行計劃--set autotrace [final]SQL
- oracle10g 檢視SQL執行計劃OracleSQL
- 透過查詢檢視sql執行計劃SQL
- sql中使用函式導致explain plan for和set autotrace得到執行計劃不準確SQL函式AI