[20220517]toad使用gather_plan_statistics提示問題.txt

lfree發表於2022-05-17

[20220517]toad使用gather_plan_statistics提示問題.txt

--//這個問題以前應該遇到過,再次做一次記錄。

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下執行:
Select /*+ gather_plan_statistics */ * from dept --where rownum=1;

--//在toad下檢視執行計劃:
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: 2022/5/17 9:44:06

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 */ * from dept --where rownum=1']
outHash=[0.178956209e+010]


Elapsed time: 0.006

--------------------------------------------------------------------------------
Timestamp: 2022/5/17 9:44:06

Select *
from v$sql_plan
Where hash_value = '1789562090'
and child_number =0
order by id

--//如果查詢hash_value = '1789562090',沒有結果,這樣toad只能使用explain plan 解析執行計劃。
--//為什麼toad執行看不到開啟gather_plan_statistics的真實執行計劃呢。

--//如果掃描共享池子可以發現,實際上toad執行的是:

SCOTT@book> select sql_id,sql_text from v$sqlarea where sql_text like '%gather_plan_statistics%';
SQL_ID        SQL_TEXT
------------- ------------------------------------------------------------
0yb7hx933y5z0 Select /*+ gather_plan_statistics */ * from dept --where row
              num=1;;

daymukjvv6pxz select sql_id,sql_text from v$sqlarea where sql_text like '%
              gather_plan_statistics%'


--//注意看實際上toad在後面又加了1個分號,是兩個分號。
SCOTT@book> @ dpc 0yb7hx933y5z0 '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  0yb7hx933y5z0, child number 0
-------------------------------------
Select /*+ gather_plan_statistics */ * from dept --where rownum=1;;
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 |
--------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / DEPT@SEL$1

--//這樣看到才是開啟gather_plan_statistics 的執行計劃。
--//遇到這樣的情況最簡單的方法就是把分號單獨一行,注意這樣寫還是不行。
Select /*+ gather_plan_statistics */ * from dept --where rownum=1;
;

--//寫成如下ok。
Select /*+ gather_plan_statistics */ * from dept --where rownum=1
;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2894783/,如需轉載,請註明出處,否則將追究法律責任。

相關文章