[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
- 從oracle v$version檢視中查詢os的資訊Oracle
- 【檢視】深入探究 V$PARAMETER的底層查詢內幕
- 檢視查詢報錯
- 【MySQL】檢視&子查詢MySql
- oracle 常用查詢檢視Oracle
- v$session/v$process檢視涉及的相關會話資訊的查詢Session會話
- oracle10g中部分檢視查詢非常‘慢“問題解決Oracle
- Oracle效能問題檢查 - 常用查詢指令碼(final)Oracle指令碼
- 檢視慢查詢進度
- [20170502]11G查詢隱含引數檢視.txt
- SGA中Latch 的分類和查詢--結合v$latch檢視
- 儲存過程中查詢資料字典檢視(v$或dba)儲存過程
- 【PDB】Oracle跨PDB檢視查詢Oracle
- MySQL 查詢的成本的檢視MySql
- OushuDB 檢視查詢執行情況
- 【MV】物化檢視查詢重寫
- sql查詢檢視列備註SQL
- 前端問題檢查前端
- 【Oracle九大效能檢視】之3.v$session_關於與其它效能檢視表聯合查問題SQLOracleSessionSQL
- 使用物化檢視查詢重寫 優化對於 UNION ALL檢視的CONNECT BY查詢優化
- [20120307]檢視v$session檢視的定義.txtSession
- [20150727]使用標量子查詢小問題.txt
- 物化檢視中的統計資訊導致的查詢問題分析和修復
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- sql 模糊查詢問題SQL
- Xilinx問題查詢
- 斷號查詢問題
- 檢視 Laravel 查詢資料語句Laravel
- Linux查詢檢視幫助命令Linux
- 查詢基表的相關檢視
- 【Oracle】如何查詢檢視時使用索引Oracle索引
- 資料庫的查詢與檢視資料庫
- [20130125]利用v$active_session_history檢視解決資料庫問題.txtSession資料庫
- [20210528]V$INDEXED_FIXED_COLUMN檢視.txtIndex
- RAC:在子查詢使用gv$檢視,有時查詢不出資料
- mysql開啟檢視慢查詢日誌MySql
- [20211220]關於標量子查詢問題.txt