[20210418]查詢v$檢視問題.txt
[20210418]查詢v$檢視問題.txt
--//昨天看連結,實際上類似的問題我自
--//己也遇到過。x需要在會話級別上設定cursor_sharing =force。裡面有一段程式碼:
-- Get user SID information
SELECT SID
INTO v_sid
FROM v$mystat
WHERE ROWNUM = 1;
-- Get Program executable,OSUSER Details,Machine Details for this session
SELECT LOWER (program)
,osuser
,machine
,module
INTO v_exe
,v_osuser
,v_machine
,v_module
FROM v$session
WHERE SID = v_sid;
--//我當時修改如下:
SELECT LOWER (program)
,osuser
,machine
,module
INTO v_exe
,v_osuser
,v_machine
,v_module
FROM v$session
WHERE SID = ( SELECT SID FROM v$mystat WHERE ROWNUM = 1);
--//一樣是很"慢",注意這裡慢要打上引號,實際上在awr報表上能看到我改寫的語句(以前看不到)。實際上我在最佳化過程中就出現了鏈
--//接看到的奇怪現象,我後來還是分開寫。
http://blog.itpub.net/267265/viewspace-2740080/ =>[20201204]為什麼返回2行記錄.txt
http://blog.itpub.net/267265/viewspace-2740213/ =>[20201208]為什麼返回2行記錄補充.txt
1.環境:
SYS@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
SYS@book> @ spid
SID SERIAL# PROCESS SERVER SPID PID P_SERIAL# C50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
58 13 47135 DEDICATED 47136 28 6 alter system kill session '58,13' immediate;
2.測試:
SYS@book> select * from v$session where sid=58;
Plan hash value: 1627146547
--------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| | | |
| 1 | MERGE JOIN CARTESIAN | | 1 | 1378 | 0 (0)| | | |
| 2 | NESTED LOOPS | | 1 | 370 | 0 (0)| | | |
|* 3 | FIXED TABLE FIXED INDEX| X$KSLWT (ind:1) | 1 | 161 | 0 (0)| | | |
|* 4 | FIXED TABLE FIXED INDEX| X$KSLED (ind:2) | 1 | 209 | 0 (0)| | | |
| 5 | BUFFER SORT | | 1 | 1008 | 0 (0)| 2048 | 2048 | 2048 (0)|
|* 6 | FIXED TABLE FIXED INDEX| X$KSUSE (ind:1) | 1 | 1008 | 0 (0)| | | |
--------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$5C160134
3 - SEL$5C160134 / W@SEL$3
4 - SEL$5C160134 / E@SEL$3
6 - SEL$5C160134 / S@SEL$3
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("W"."KSLWTSID"=58)
4 - filter("W"."KSLWTEVT"="E"."INDX")
6 - filter(("S"."INDX"=58 AND "S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0
AND BITAND("S"."KSUSEFLG",1)<>0))
SYS@book> select * from v$session where sid=userenv('SID');
Plan hash value: 2422122865
--------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| | | |
| 1 | MERGE JOIN CARTESIAN | | 1 | 1378 | 0 (0)| | | |
| 2 | NESTED LOOPS | | 1 | 370 | 0 (0)| | | |
|* 3 | FIXED TABLE FULL | X$KSLWT | 1 | 161 | 0 (0)| | | |
|* 4 | FIXED TABLE FIXED INDEX| X$KSLED (ind:2) | 1 | 209 | 0 (0)| | | |
| 5 | BUFFER SORT | | 1 | 1008 | 0 (0)| 2048 | 2048 | 2048 (0)|
|* 6 | FIXED TABLE FULL | X$KSUSE | 1 | 1008 | 0 (0)| | | |
--------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$5C160134
3 - SEL$5C160134 / W@SEL$3
4 - SEL$5C160134 / E@SEL$3
6 - SEL$5C160134 / S@SEL$3
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("W"."KSLWTSID"=USERENV('SID'))
4 - filter("W"."KSLWTEVT"="E"."INDX")
6 - filter(("S"."INDX"=USERENV('SID') AND "S"."INST_ID"=USERENV('INSTANCE') AND
BITAND("S"."KSSPAFLG",1)<>0 AND BITAND("S"."KSUSEFLG",1)<>0))
--//你可以發現id =3, oracle沒有把USERENV('SID')當作常量處理。
--//使用繫結變數呢?
SYS@book> variable n number;
SYS@book> exec :n := 58
PL/SQL procedure successfully completed.
SYS@book> select * from v$session where sid= :n;
Plan hash value: 1627146547
--------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| OMem | 1Mem | Used-Mem |
--------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| | | |
| 1 | MERGE JOIN CARTESIAN | | 1 | 1378 | 0 (0)| | | |
| 2 | NESTED LOOPS | | 1 | 370 | 0 (0)| | | |
|* 3 | FIXED TABLE FIXED INDEX| X$KSLWT (ind:1) | 1 | 161 | 0 (0)| | | |
|* 4 | FIXED TABLE FIXED INDEX| X$KSLED (ind:2) | 1 | 209 | 0 (0)| | | |
| 5 | BUFFER SORT | | 1 | 1008 | 0 (0)| 2048 | 2048 | 2048 (0)|
|* 6 | FIXED TABLE FIXED INDEX| X$KSUSE (ind:1) | 1 | 1008 | 0 (0)| | | |
--------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$5C160134
3 - SEL$5C160134 / W@SEL$3
4 - SEL$5C160134 / E@SEL$3
6 - SEL$5C160134 / S@SEL$3
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("W"."KSLWTSID"=:N)
4 - filter("W"."KSLWTEVT"="E"."INDX")
6 - filter(("S"."INDX"=:N AND "S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0
AND BITAND("S"."KSUSEFLG",1)<>0))
--//OK.
--//作者給出了一個物化檢視的方式解決這類問題:
with mysid as ( select /*+ materialize */ userenv('SID') n from dual )
select * from v$session, mysid
where sid = n;
with mysid as
( select /*+ result_cache */ userenv('SID') n from dual )
select * from v$session, mysid
where sid = n;
--//另外這樣寫也不是最優的:
with mysid as ( select /*+ materialize */ userenv('SID') n from dual )
select * from v$session
where sid in (select n from mysid);
--//作者給出一個建議:
If you're querying the V$ views, it is always worth generating an execution plan and double-checking that you are
getting any performance benefits you can, because those memory structures could be a lot larger than you expect
depending on your configuration
如果您正在查詢V$檢視,生成執行計劃並反覆檢查您是否能獲得了任何效能好處總是值得的,因為根據您的配置,這些記憶體結構可能比您
預期的要大得多。
--//也就是好好看看你的執行計劃。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2768591/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20211214]檢視檢視V$ACTIVE_SESSION_HISTORY遇到奇怪問題.txtSession
- [20211220]關於標量子查詢問題.txt
- [20211206]toad下job建立檢視問題.txt
- [20210208]lob欄位與查詢的問題.txt
- [20211019]V$DETACHED_SESSION檢視.txtSession
- 檢視查詢報錯
- 【MySQL】檢視&子查詢MySql
- [20221128]再談防水牆(檢視訪問效能問題).txt
- [20230308]12c以上版本模糊查詢問題.txt
- [20210528]V$INDEXED_FIXED_COLUMN檢視.txtIndex
- [20180907]訪問v$檢視與一致性讀取.txt
- [20210114]toad檢視真實執行計劃問題.txt
- [20231017]使用dbms_xplan.display_awr查詢遇到的問題.txt
- OushuDB 檢視查詢執行情況
- 【PDB】Oracle跨PDB檢視查詢Oracle
- MySQL 查詢的成本的檢視MySql
- [20231026]bbed檢視索引kd_off結構的問題.txt索引
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- [20220308]查詢x$ksmmem遇到的疑問.txt
- [20181103]12c檢視V$EVENT_NAME.txt
- [20201207]12c v$open_cursor檢視.txt
- sql 模糊查詢問題SQL
- 檢視 Laravel 查詢資料語句Laravel
- [20210418]CBC latch再討論3.txt
- [20210418]開啟多個程式執行.txt
- leetcode題解(查詢表問題)LeetCode
- [20190320]關於使用smem檢視記憶體使用的問題.txt記憶體
- [20210205]toad檢視真實執行計劃問題3.txt
- [20221130]測試訪問檢視v$session幾種情況的效能差異.txtSession
- Laravel5.7 查詢問題Laravel
- sphinx查詢過濾問題
- oracle資料庫sql查詢檢視第二次查詢很慢Oracle資料庫SQL
- [20220309]查詢x$ksmmem遇到的疑問補充.txt
- [20180926]查詢相似索引.txt索引
- SQLAlchemy in 查詢空列表問題分析SQL
- 如何使用SQL查詢檢視,Postico使用技巧分享~SQL
- 【SQL】Oracle查詢轉換之檢視合併SQLOracle
- 解決pod健康檢查問題