我正在为 Oracle SE 创建用于数据库锁定监视的定制工具。基本上我有调度程序工作,它每 10 秒拍摄一次 gv$session 信息的快照,并将其存储在定制的 ashstat_data 表中。
现在,为了方便起见,我正在尝试创建一个视图以分层格式列出会话阻塞链。该视图基于分层选择,将阻塞会话显示为阻塞会话父项下的子项:
with l_snap as
(select *
from ashstat_data
where sample_time>sysdate - interval '10' second )
SELECT
s.sample_time,
' '||LPAD('-', (LEVEL-1), '-' ) || SESSION_ID asid,
s.wait_class,
s.event,
s.LOGON_TIME,
s.sql_id
FROM l_snap s
natural join dba_users d
inner join dba_objects do on s.LOCKED_OBJECT_ID=do.object_id
WHERE s.SESSION_ID IN
(SELECT blocking_session FROM l_snap where blocking_session is not null)
OR s.blocking_session IS NOT NULL
CONNECT BY PRIOR
s.SESSION_ID=s.blocking_session
START WITH s.blocking_session IS NULL
;
到目前为止,只有当我从单个快照(where sample_time>sysdate - interval '10' second
)中选择数据时,它才能完美运行:
SAMPLE_TIME ASID WAIT_CLASS LOGON_TIME SQL_ID
5/9/2018 16:13:08.173 302 Idle 05/09/2018 11:33:57
5/9/2018 16:13:08.173 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:13:08.173 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx
但是,如果我从多个快照中选择记录(即(where sample_time>sysdate - interval '12' second
)),它会根据条件连接来排列它们:
SAMPLE_TIME ASID WAIT_CLASS LOGON_TIME SQL_ID
5/9/2018 16:17:18.166 302 Idle 05/09/2018 11:33:57
5/9/2018 16:17:18.166 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:17:08.170 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:17:18.166 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx
5/9/2018 16:17:08.170 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx
5/9/2018 16:17:08.170 302 Idle 05/09/2018 11:33:57
5/9/2018 16:17:18.166 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:17:08.170 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:17:18.166 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx
5/9/2018 16:17:08.170 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx
我想要实现的是为每个快照单独表示阻塞链,因此对于整个收集的数据,最后一个选择结果看起来像这样:
SAMPLE_TIME ASID WAIT_CLASS LOGON_TIME SQL_ID
5/9/2018 16:17:18.166 302 Idle 05/09/2018 11:33:57
5/9/2018 16:17:18.166 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:17:18.166 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx
5/9/2018 16:17:08.170 302 Idle 05/09/2018 11:33:57
5/9/2018 16:17:08.170 -594 Application 05/09/2018 11:34:01 0wvbggc3p3swx
5/9/2018 16:17:08.170 -646 Application 05/09/2018 11:34:07 0wvbggc3p3swx