[20210205]toad檢視真實執行計劃問題3.txt
[20210205]toad檢視真實執行計劃問題3.txt
--//前段時間遇到toad檢視真實執行計劃問題的問題,實際上最近還遇到一種情況,做一個記錄.
1.環境:
SCOTT@book> @ ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
2.測試:
--//在toad的sql編輯介面上,輸入如下:
select /*+ gather_plan_statistics */ deptno,dname,loc from dept
order by dname
--//看到的執行計劃如下:
Plan hash value: 120787663
---------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | Reads | OMem | 1Mem | Used-Mem |
---------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 4 (100)| | 4 |00:00:00.01 | 6 | 5 | | | |
| 1 | SORT ORDER BY | | 1 | 4 | 80 | 4 (25)| 00:00:01 | 4 |00:00:00.01 | 6 | 5 | 2048 | 2048 | 2048 (0)|
| 2 | TABLE ACCESS FULL| DEPT | 1 | 4 | 80 | 3 (0)| 00:00:01 | 4 |00:00:00.01 | 6 | 5 | | | |
---------------------------------------------------------------------------------------------------------------------------------------------------------
--//在toad的sql編輯介面上,輸入如下:
select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname;
--//看到的執行計劃如下:
Plan hash value: 3383998547
---------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 80 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
--//很明顯這樣看到的執行計劃不是真實的執行計劃,因為我加入提示gather_plan_statistics.使用toad自帶的SQL Tracker看看.
--------------------------------------------------------------------------------
Timestamp: 2021/2/5 15:34:35
declare
v_ignore raw(100);
v_oldhash number;
v_hash number;
begin
v_hash := dbms_utility.get_sql_hash(:SQLText || chr(0), v_ignore, v_oldhash);
:outHash := v_hash;
end;
SQLText=['select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname']
outHash=[0.201803003e+010]
Elapsed time: 0.001
--------------------------------------------------------------------------------
Timestamp: 2021/2/5 15:34:36
Select *
from v$sql_plan
Where hash_value = '2018030035'
and child_number =0
order by id
sqlhv=['2018030035']
cn=[0]
Elapsed time: 0.002
--------------------------------------------------------------------------------
Timestamp: 2021/2/5 15:34:36
explain plan set statement_id='Administrator:020521153435' into SYS.PLAN_TABLE$ For select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname
Elapsed time: 0.004
--------------------------------------------------------------------------------
Timestamp: 2021/2/5 15:34:36
Select *
From SYS.PLAN_TABLE$
Where statement_id = 'Administrator:020521153435'
order by id
STATEMENT_ID=['Administrator:020521153435']
Elapsed time: 0.003
--------------------------------------------------------------------------------
Timestamp: 2021/2/5 15:34:36
select * from table(dbms_xplan.display(table_name => 'SYS.PLAN_TABLE$',statement_id => 'Administrator:020521153435', format => 'ADVANCED, ALLSTATS, LAST, OUTLINE, PEEKED_BINDS'))
--------------------------------------------------------------------------------
Timestamp: 2021/2/5 15:15:47
--//為什麼計算hash_value錯誤呢?查詢共享池知道對於的sql_id.
SCOTT@book> @ sql_id 6pdk4km3kwfz1
SQL_ID SQLTEXT
------------- ---------------------------------------------------------------
6pdk4km3kwfz1 select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname;
--//注意看一個小細節,就是後面的分號.在前面的hash_value計算時不存在.
--//如果你寫成如下,沒有最後的分號.
select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname
--//看到的執行計劃就是如下:
Plan hash value: 3383998547
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 3 (100)| | 4 |00:00:00.01 | 6 |
| 1 | TABLE ACCESS FULL| DEPT | 1 | 4 | 80 | 3 (0)| 00:00:01 | 4 |00:00:00.01 | 6 |
--------------------------------------------------------------------------------------------------------------------
--//這個是真實的執行計劃.
--//如下現在改寫如下,就是寫回分號:
select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname;
--//看到的執行計劃就是如下:
Plan hash value: 3383998547
--------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 3 (100)| | 4 |00:00:00.01 | 6 |
| 1 | TABLE ACCESS FULL| DEPT | 1 | 4 | 80 | 3 (0)| 00:00:01 | 4 |00:00:00.01 | 6 |
--------------------------------------------------------------------------------------------------------------------
--//有點奇怪吧,現在能看到真實的執行計劃.實際上主要沒有後面的分號已經存在共享池裡面,這樣就可以看到"真實"的執行計劃,
--//但是實際上現在執行語句的sql_id='6pdk4km3kwfz1'.可以簡單驗證如下:
SCOTT@book> select sql_id,sql_text,hash_value,executions from v$sqlarea where sql_id in ('8zt25wdw4jcfm','6pdk4km3kwfz1');
SQL_ID SQL_TEXT HASH_VALUE EXECUTIONS
------------- ------------------------------------------------------------ ---------- ----------
6pdk4km3kwfz1 select /*+ gather_plan_statistics */ deptno,dname,loc from d 3341695969 3
ept --1 --order by dname;
8zt25wdw4jcfm select /*+ gather_plan_statistics */ deptno,dname,loc from d 2018030035 1
ept --1 --order by dname
--//在toad介面下執行:
select /*+ gather_plan_statistics */ deptno,dname,loc from dept
--1
--order by dname;
SCOTT@book> select sql_id,sql_text,hash_value,executions from v$sqlarea where sql_id in ('8zt25wdw4jcfm','6pdk4km3kwfz1');
SQL_ID SQL_TEXT HASH_VALUE EXECUTIONS
------------- ------------------------------------------------------------ ---------- ----------
6pdk4km3kwfz1 select /*+ gather_plan_statistics */ deptno,dname,loc from d 3341695969 4
ept --1 --order by dname;
8zt25wdw4jcfm select /*+ gather_plan_statistics */ deptno,dname,loc from d 2018030035 1
ept --1 --order by dname
--//sql_id=6pdk4km3kwfz1的EXECUTIONS增加.在toad介面看到的執行計劃實際上是sql_id=8zt25wdw4jcfm.
3.另外還有一種可能導致看不到真實的執行計劃:
--//如果執行看前面的SQL Tracker看看.它執行的是child_number =0的情況,如果它不存在看到的執行計劃也不真實.
Select *
from v$sql_plan
Where hash_value = '181301342'
and child_number =0
order by id
4.總結:
--//這個問題主要在於toad下可以不輸入後面的分號就可以執行,它自動不上,如果最後一行是註解,這樣解析就出錯了.
--//解決方法很簡單,刪除最後的分號或者在前一行的非註解行加入分號也可以.例子如下:
select /*+ gather_plan_statistics */ deptno,dname,loc from dept;
--1
--order by dname;
--//以前遇到的情況就是會話cursor_sharing=force.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2756361/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20210114]toad檢視真實執行計劃問題.txt
- [20181120]toad看真實的執行計劃.txt
- Oracle如何檢視真實執行計劃(一)Oracle
- [20240313]toad gather_plan_statistics執行計劃相關問題.txt
- 檢視 OceanBase 執行計劃
- [20230130]toad看執行計劃注意.txt
- [20211206]toad下job建立檢視問題.txt
- [20210205]警惕toad下優化直方圖相關sql語句3.txt優化直方圖SQL
- 如何檢視SQL的執行計劃SQL
- Oracle檢視執行計劃的命令Oracle
- [20220414]toad呼叫執行指令碼問題.txt指令碼
- 執行計劃-2:檢視更多的資訊
- 在Oracle中,如何得到真實的執行計劃?Oracle
- 使用set autotrace on 檢視資料庫執行計劃資料庫
- 達夢資料庫SQL執行計劃檢視方法資料庫SQL
- 檢視SQL執行計劃的幾種常用方法YQSQL
- 檢視執行計劃出現ORA-22992錯誤
- Oracle檢視sql_id 的歷史執行計劃OracleSQL
- 微課sql最佳化(11) 、如何檢視執行計劃SQL
- SQL執行計劃異常引起的效能問題SQL
- SQL執行計劃異常 引起的效能問題SQL
- 檢視一個正在執行的sql的執行計劃(explain for connection processlist_id)SQLAI
- 達夢資料庫獲取SQL真實的執行計劃資料庫SQL
- [20210926]並行執行計劃疑問.txt並行
- 執行計劃-1:獲取執行計劃
- SQL優化案例-從執行計劃定位SQL問題(三)SQL優化
- Oracle優化案例-從執行計劃定位SQL問題(三)Oracle優化SQL
- 從真實案例出發,全方位解讀 NebulaGraph 中的執行計劃
- app 自動化 (真機)+jenkins 進行執行問題APPJenkins
- SQL最佳化案例-從執行計劃定位SQL問題(三)SQL
- toad使用(檢視alert日誌、Database Report)Database
- 【SQL_PLAN】Oracle 透過檢視sql_plan 格式化執行計劃SQLOracle
- 分析執行計劃優化SQLORACLE的執行計劃(轉)優化SQLOracle
- [20210205]警惕toad下優化直方圖相關sql語句.txt優化直方圖SQL
- [20181206]toad 12小問題.txt
- SYBASE執行計劃
- MySQL 執行計劃MySql
- MySQL執行計劃MySql