这里通过 Oracle 查询锁之间的依赖关系 的SQL 查询生产环境锁;
由于 Oracle 查询锁之间的依赖关系 查询数据比较全(例如:锁和被锁关系,锁的对象,等待事件,操作锁的客户端,会话跑的sql,等状态.) 若在每个SQL锁的对象较多、操作人较多、且最源头锁对象的sid 不断变化时;想通过 Oracle 查询锁之间的依赖关系 再右眼查到最源头锁sid比较费事。
所以特写如下SQL实现 Oracle 查找锁之间依赖关系的最源头SID (首先得到锁的SID),再通过SID找sql查问题..
*** 思路 ***
1、证明有锁,需要满足v$session.BLOCKING_SESSION[阻塞会话的SID] IS NOT NULL and BLOCKING_SESSION_STATUS = 'VALID'条件;
2、阻塞会话的SID 不在 SID 中就是最源头的SID (首先得到锁的SID);
3、注意:v$session单实例,gv$session 多节点;
SQL:
WITH LOCK_1 AS
( SELECT DISTINCT S.INST_ID, S.SID, S.BLOCKING_SESSION, S.LAST_CALL_ET
FROM GV$SESSION S
WHERE S.BLOCKING_SESSION IS NOT NULL
and BLOCKING_SESSION_STATUS = 'VALID' )
SELECT DISTINCT BLOCKING_SESSION
FROM LOCK_1
WHERE BLOCKING_SESSION NOT IN ( SELECT SID FROM LOCK_1);
--------------------------------------------------------------------------------
本文用到的2个字段:
V$SESSION displays session information for each current session.
SID NUMBER Session identifier
BLOCKING_SESSION_STATUS VARCHAR2(11) This column provides details on whether there is a blocking session:
VALID - there is a blocking session, and it is identified in the BLOCKING_INSTANCEand BLOCKING_SESSION columns
NO HOLDER - there is no session blocking this session
NOT IN WAIT - this session is not in a wait
UNKNOWN - the blocking session is unknown
BLOCKING_SESSION NUMBER Session identifier of the blocking session. This column is valid only ifBLOCKING_SESSION_STATUS has the valueVALID.
v$session 官网注释 ==
** SQL写的不是很智能,但能实现我的目的! **