数据库中如何查看历史会话等待事件对应的session信息

84次阅读
没有评论

共计 2463 个字符,预计需要花费 7 分钟才能阅读完成。

丸趣 TV 小编给大家分享一下数据库中如何查看历史会话等待事件对应的 session 信息,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

此处以 enq: TX – row lock contention 等待时间为例。

如果在此回话发生在 awr 快照信息默认的保存天数以内。
可以通过如下 sql 查询到相关的 session 信息。
select * from DBA_HIST_ACTIVE_SESS_HISTORY where event like %enq: TX – row lock contention%

DBA_HIST_ACTIVE_SESS_HISTORY 中的 blocking_session 字段关联 DBA_HIST_ACTIVE_SESS_HISTORY 中的 session_id 找到对应的 sql_id 从而得到回话信息。

可以通过如下查询直接获取信息:
select t.instance_number,
       t.sample_time,
       lpad(– , 2 * (level – 1), – ) || t.client_id,
       t.session_id,
       t.blocking_session,
       t.session_serial#,
       t.sql_id,
       t.event,
       t.session_state,
       level,
       connect_by_isleaf,
       connect_by_iscycle
  from dba_hist_active_sess_history  t
 where snap_id between 36878 and 36879
 start with blocking_session is not null
        and event like enq: TX – row lock contention%
connect by nocycle sample_time = prior sample_time
       and session_id = prior blocking_session
       and session_serial# = prior blocking_session_serial#

其中 blocking session 为正在阻塞该回话的 session

实战案例:
查看等待事件为行锁的 session
select a.snap_id,
       a.sql_id,
       a.session_id,
       a.session_serial#,
       a.blocking_session,
       a.blocking_session_serial#,
       a.blocking_session_status
  from DBA_HIST_ACTIVE_SESS_HISTORY a
 where event like %enq: TX – row lock contention%
   and snap_id between 20399 and 20400

编写子查询,查看阻塞回话,并统计阻塞次数
select a.blocking_session,
       a.blocking_session_serial#,
       count(a.blocking_session)
  from DBA_HIST_ACTIVE_SESS_HISTORY a
 where event like %enq: TX – row lock contention%
   and snap_id between 20399 and 20400
 group by a.blocking_session, a.blocking_session_serial#
 order by 3 desc
 
查看阻塞回话的 sql_id 和被阻塞的 sql_id,条件为阻塞大于 19 次的
select distinct b.sql_id,c.blocked_sql_id
  from DBA_HIST_ACTIVE_SESS_HISTORY b,
       (select a.sql_id as blocked_sql_id,
       a.blocking_session,
               a.blocking_session_serial#,
               count(a.blocking_session)
          from DBA_HIST_ACTIVE_SESS_HISTORY a
         where event like %enq: TX – row lock contention%
           and snap_id between 20399 and 20400
         group by a.blocking_session, a.blocking_session_serial#,a.sql_id
        having count(a.blocking_session) 19
         order by 3 desc) c
 where b.session_id = c.blocking_session
   and b.session_serial# = c.blocking_session_serial#
   and b.snap_id between 20399 and 20400

动态性能视图注释:

V$ACTIVE_SESSION_HISTORY displays sampled session activity in the database. It contains snapshots of active database sessions taken once a second. A database session is considered active if it was on the CPU or was waiting for an event that didn t belong to the Idle wait class. Refer to the V$EVENT_NAME view for more information on wait classes.

看完了这篇文章,相信你对“数据库中如何查看历史会话等待事件对应的 session 信息”有了一定的了解,如果想了解更多相关知识,欢迎关注丸趣 TV 行业资讯频道,感谢各位的阅读!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-24发表,共计2463字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)