Oracle動態視圖v$active_session_history實戰示例

Oracle動態視圖實戰之v$active_session_history

先看下官方解釋

  • Samples of wait event information are taken once per second and made available using the V$ACTIVE_SESSION_HISTORY view. An active session is one that is waiting on CPU or any event that does not belong to the "Idle" wait class at the time of the sample. The sample information is written to a circular buffer in the SGA, so the greater the database activity, the less time the information will remain available for.
  • 有幾個關鍵點:1秒采集一次,執行時間很快遠小於1秒的SQL基本不會采集到,隻寫入非空閑狀態的事件,循環存放活動越多保存的時間就越短。

實際工作中主要應用

v$active_session_history的字段非常豐富,實際工作中主要應用在下面這些情況:

a.應用場景:開發反應2023-03-02 00:22至00:35,數據落盤慢,根據情況查看此時間段的主要活動事件,數量,與sql_id(全局)
select count(*), sql_id, event, blocking_session
  from gv$active_session_history
 where sample_time between
       to_date('2023-03-02 00:22:00', 'yyyy-mm-dd hh24:mi:ss') and
       to_date('2023-03-02 00:35:00', 'yyyy-mm-dd hh24:mi:ss')
 group by sql_id, event, blocking_session
 order by 1;
(非全局)BLOCKING_INST_ID--被阻塞者, blocking_session--阻塞者
select count(*), sql_id, event, BLOCKING_INST_ID, blocking_session
  from v$active_session_history
 where sample_time between
       to_date('2023-03-02 00:20:00', 'yyyy-mm-dd hh24:mi:ss') and
       to_date('2023-03-02 00:35:00', 'yyyy-mm-dd hh24:mi:ss')
 group by sql_id, event, BLOCKING_INST_ID, blocking_session
 order by 1;
b.現在我們已經得到兩個關鍵信息:sql_id與阻塞事件,首先根據sql_id我們可以再進一步使用此視圖,實際中可以多調整幾個較小的時間段,以突出最有代表的信息
select count(*),
       session_id,
       session_serial#,
       sql_id,
       event,
       BLOCKING_INST_ID,
       blocking_session
  from v$active_session_history
 where sample_time between
       to_date('2023-03-02 00:24:00', 'yyyy-mm-dd hh24:mi:ss') and
       to_date('2023-03-02 00:25:00', 'yyyy-mm-dd hh24:mi:ss')
   and sql_id = '1xfbtdvu3xb67'
 group by session_id,
          session_serial#,
          sql_id,
          event,
          BLOCKING_INST_ID,
          blocking_session
 order by 3;
c.加入等待事件後更清晰
select count(*),
       session_id,
       sql_id,
       event,
       BLOCKING_INST_ID,
       blocking_session
  from v$active_session_history
 where sample_time between
       to_date('2023-03-02 00:25:00', 'yyyy-mm-dd hh24:mi:ss') and
       to_date('2023-03-02 00:35:00', 'yyyy-mm-dd hh24:mi:ss')
   and event = 'library cache lock'
   and sql_id = '1j47z0mc6k02b'
 group by session_id, sql_id, event, BLOCKING_INST_ID, blocking_session
 order by 1;
結論:可以看出大量並發等待,最終是發現有什麼阻塞瞭此SQL語句

結合我們的AWR報告

當然也要結合我們的AWR報告:(兩份為同時間段,上一份為有爭用,下一份為正常情況,報告太長,隻截取瞭關鍵點)

關鍵點

最後關鍵點a:下面報告裡的sql_id與事件與v$active_session_history裡查出來的結果相同,進一步證明事件與此SQL的關聯性。

  • 總結時間:

我們根據SQL_ID找到相應的SQL語句,從而找到對應的TABLE,最終對應到兩張分區表,分別為:AA_BBB_CCCC_DDDD_OUT,AA_BBB_CCCC_DDDD_IN。

在對開發進行嚴刑拷打逼問後(如果開發小哥不松口怎麼辦?下節預告:可以直接查詢時間段的DDL語句執行情況),終於告訴我當天晚上時間點上對這兩張表做瞭大量新建分區表的操作,至此基本水落石出。

#根據dba_objects確定創建時間是否匹配
select owner,
       object_name,
       object_type,
       to_char(created, 'yyyy-mm-dd hh24:mi:ss')
  from dba_objects
 where object_name = 'AA_BBB_CCCC_DDDD_OUT'
   and created > to_date('2023-03-01', 'yyyy-mm-dd')
 order by 4;
 select owner,
       object_name,
       object_type,
       to_char(created, 'yyyy-mm-dd hh24:mi:ss')
  from dba_objects
 where object_name = 'AA_BBB_CCCC_DDDD_IN'
   and created > to_date('2023-03-01', 'yyyy-mm-dd')
 order by 4;

最後關鍵點b:我一定要記住,應該最先查看OSWatch的數據,排除OS的問題。至於OSW怎麼部署,運行和查看以後章節再補充。同時也得查看database的alert.log日志,有驚喜╰(°▽°)╯

以上就是Oracle動態視圖v$active_session_history實戰示例的詳細內容,更多關於Oracle動態視圖的資料請關註WalkonNet其它相關文章!

推薦閱讀: