查看历史会话等待事件对应的session信息

时间:2023-03-09 21:23:56
查看历史会话等待事件对应的session信息

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

查看snap_id对应时间

select to_char(s.startup_time,'dd Mon "at" HH24:mi:ss') instart_fmt

, di.instance_name inst_name

, di.db_name db_name

, s.snap_id snap_id

, to_char(s.end_interval_time,'dd Mon YYYY HH24:mi') snapdat

, s.snap_level lvl

from dba_hist_snapshot s

, dba_hist_database_instance di

where di.dbid = s.dbid

and di.instance_number = s.instance_number

and di.startup_time = s.startup_time

order by db_name, instance_name, snap_id;

如果在此回话发生在awr快照信息默认的保存天数以内。

可以通过如下sql查询到相关的session信息。

select * from DBA_HIST_ACTIVE_SESS_HISTORY where event like '%enq: TX - row lock contention%'

select * from dba_hist_active_sess_history WHERE event like 'enq: TX - row lock contention%' order by SAMPLE_TIME DESC

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.

这个视图三思老师的博客里将的比较清楚:

http://blog.itpub.net/7607759/viewspace-617178

This view contains one row for each active session per sample and returns the latest session sample rows first. A majority of the columns describing the session in the active session history are present in the V$SESSION view.

DBA_HIST_ACTIVE_SESS_HISTORY displays the history of the contents of the in-memory active session history of recent system activity. This view contains snapshots of V$ACTIVE_SESSION_HISTORY. See "V$ACTIVE_SESSION_HISTORY" for further interpretation details for many of these columns (except SNAP_ID, DBID, and INSTANCE_NUMBER).