首頁 > 軟體

Oracle動態檢視v$active_session_history實戰範例

2023-03-09 06:00:14

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動態檢視的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com