oracle sql monitor
前言
oracle的性能監測工具除了AWR、ASH、ADDM,還可以參考oracle的動態性能視圖:v$active_session_history (dba_hist_active_sess_history)、v$sqlstats(dba_hist_sqlstat)、v$sqlarea、v$session_longops等,但是這些視圖都是一段時間內的匯總,沒有單條sql的執行情況。
而sql monitor提供類似mysql 慢sql日志功能,相關的動態視圖v$sql_monitor、v$sql_plan_monitor
The V$SQL_MONITOR view contains a subset of the statistics available in V$SQL. However, unlike V$SQL, monitoring statistics are not cumulative over several executions. Instead, one entry in V$SQL_MONITOR is dedicated to a single execution of a SQL statement. If two executions of the same SQL statement
are being monitored, each of these executions will have a separate entry in V$SQL_MONITOR
To uniquely identify two executions of the same SQL statement, a composite key called an execution key is generated. This execution key is composed of three attributes, each corresponding to a column in V$SQL_MONITOR:
? SQL identifier to identify the SQL statement (SQL_ID)
? Start execution timestamp (SQL_EXEC_START)
? An internally generated identifier to ensure that this primary key is truly
unique (SQL_EXEC_ID)
一、確認sql monitor是否開啟
默認的,statistics_level和control_management_pack_access為TYPICAL和DIAGNOSTIC+TUNING,即自動開啟sql monitor。
二、sql被捕獲的前提
sql monitor開啟后,會自動捕獲符合下面條件之一的sql到v$sql_monitor
即:
1、sql單次執行的IO/CPU時間超過5s
2、并行執行的sql,如加 /*+parallel(t,4)*/
3、加/*+monitor*/這種強制監控的語句
4、無法修改代碼,監控某sql_id,在實例層面監控此sql
經測試,alter system set events sql_monitor的方式并無法監控sql_id,官方文檔提及此功能有待商榷,所以v$sql_monitor中的內容目前只有前3種;
因為sql monitor可以記錄超過5s的sql,這對于我們平常調優有很大的參考意義
三、與sql monitor有關的參數
v$sql_monitor等動態視圖每秒刷新一次,一些sqlmon隱含參數(不重啟即可生效)如:
_sqlmon_recycle_time 監控的語句在動態視圖中的保留時間(s)
_sqlmon_threshold 語句被監控的最小執行時間,類似mysql的long_query_time(默認為5s)
| set lines 400; col name for a30; col value for a10; col description for a80 select x.ksppinm name, y.ksppstvl value, y.ksppstdf isdefault, decode(bitand(y.ksppstvf,7),1,'MODIFIED',4,'SYSTEM_MOD','FALSE') ismod, decode(bitand(y.ksppstvf,2),2,'TRUE','FALSE') isadj, KSPPDESC description from sys.x$ksppi x, sys.x$ksppcv y where x.inst_id = userenv('Instance') and y.inst_id = userenv('Instance') and x.indx = y.indx and x.ksppinm like '%sqlmon%' order by translate(x.ksppinm, ' _', ' '); |
四、sql monitor報告查看
oracle cloud control上顯示的sql monitor信息如下
如果沒有GC,可以使用DBMS_SQLTUNE.report_sql_monitor來查看,比如
已知一個慢sql的sql_id,如果此sql執行時間超過5s,就能自動被加入v$sql_monitor,可以臨時改_sqlmon_recycle_time到300(保留5min,注意抓到sql之后需要改回默認值,一面過多占用sga資源),通過以下命令得到sql的執行id:
| select max(elapsed_time/1000),max(CPU_TIME/1000),max(QUEUING_TIME/1000) from v$sql_monitor where sql_id='6pkhg3spj2mr5'; select SQL_EXEC_ID from v$sql_monitor where elapsed_time/1000>5 and sql_id='6pkhg3spj2mr5'; |
通過sql_id和SQL_EXEC_ID得到此次執行的monitor report
| set long 10000000 set longchunksize 10000000 set linesize 200 SELECT DBMS_SQLTUNE.report_sql_monitor(sql_id => '6pkhg3spj2mr5', type => 'TEXT',SQL_EXEC_ID=>'19310619') report FROM dual; |
如果sql的執行時間小于5s大于1s,可以臨時修改_sqlmon_threshold為1,將1s以上的sql全部monitor(注意抓到sql之后需要改回默認值,一面過多占用sga資源);
如果sql的執行時間小于1s,因為_sqlmon_threshold只能為整數,就要自己根據原來的sql構造監控sql,強制加/*+monitor*/,再通過sql_id和SQL_EXEC_ID得到monitor report。
除了text,還可以得到html版的report下載查看
五、關閉sql monitor
修改隱含參數_sqlmon_threshold為0即可關閉sql monitor,防止某sql從v$sql_monitor age out,除了增大_sqlmon_recycle_time,還可以臨時關閉sql monitor(記得分析完恢復)
| 關閉: alter system set "_sqlmon_threshold"=0; 開啟: alter system set "_sqlmon_threshold"=5; |
_sqlmon_threshold設為0后,以下sql全都不再被自動監控
1、sql單次執行的IO/CPU時間超過5s
2、加hint并行執行的sql,如加 /*+parallel(t,4)*/
3、加/*+monitor*/這種強制監控的語句
參考文檔
Monitoring Database Operations
V$SQL_MONITOR
總結
以上是生活随笔為你收集整理的oracle sql monitor的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Graphics将字符串居中绘制到图
- 下一篇: sql 插入