oracle查看锁阻塞-谁阻塞了谁

一 模拟锁阻塞

#阻塞1

一个会话正在往一个大表写入大量数据的时候,另一个会话加字段:

#会话1

#会话2

会话2被阻塞了。

#阻塞2

模拟一个会话update一条记录,没提交。

另一个会话也update这一条记录:

会话2被阻塞了。

二 简单查看阻塞链

select *
  from (select a.inst_id,a.sid, a.serial#,
               a.sql_id,
               a.event,
               a.status,
               connect_by_isleaf as isleaf,
               sys_connect_by_path(SID, '<-') tree,
               level as tree_level
          from gv$session a
         start with a.blocking_session is not null
        connect by nocycle a.sid = prior a.blocking_session)
 where isleaf = 1
 order by tree_level asc;

输出示例:

TREE这里可以看到阻塞链(从右往左看),这里是:

978阻塞了729;

372阻塞了616。

三 查看具体谁阻塞了谁

SELECT DISTINCT s1.inst_id as blocking_inst_id,s1.username as blocking_username,s1.machine as blocking_machine,s1.sid as blocking_sid,s1.serial# as blocking_serial#,c1.sql_text as blocking_sql_text,s1.status as blocking_staus,s1.event as blocking_event,
s2.inst_id as waiting_inst_id,s2.username as waiting_username,s2.machine waiting_machine,s2.sid as waiting_sid,s2.serial# as waiting_serial#,c2.sql_text as waiting_sql_text,s2.status as blocking_staus,s2.event as waiting_event
  FROM gv$lock l1,
       gv$session s1,
       gv$lock l2,
       gv$session s2,
       gv$sqlarea c1,
       gv$sqlarea c2,
       gv$process b1,
       gv$process b2
 WHERE     s1.sid = l1.sid
       AND s2.sid = l2.sid
       AND s1.inst_id = l1.inst_id
       AND s2.inst_id = l2.inst_id
       AND s1.paddr = b1.addr
       AND s2.paddr = b2.addr
       AND c1.SQL_ID=s1.PREV_SQL_ID       
       AND s2.sql_hash_value = c2.hash_value
       AND l1.block > 0
       AND l2.request > 0
       AND l1.id1 = l2.id1
       AND l1.id2 = l2.id2
UNION
SELECT DISTINCT s1.inst_id as blocking_inst_id,s1.username as blocking_username,s1.machine as blocking_machine,s1.sid as blocking_sid,s1.serial# as blocking_serial#,c1.sql_text as blocking_sql_text,s1.status as blocking_staus,s1.event as blocking_event,
s2.inst_id as waiting_inst_id,s2.username as waiting_username,s2.machine waiting_machine,s2.sid as waiting_sid,s2.serial# as waiting_serial#,c2.sql_text as waiting_sql_text,s2.status as blocking_staus,s2.event as waiting_event
  FROM gv$lock l1,
       gv$session s1,
       gv$lock l2,
       gv$session s2,
       gv$sqlarea c1,
       gv$sqlarea c2,
       gv$process b1,
       gv$process b2
 WHERE     s1.sid = l1.sid
       AND s2.sid = l2.sid
       AND s1.inst_id = l1.inst_id
       AND s2.inst_id = l2.inst_id
       AND s1.paddr = b1.addr
       AND s2.paddr = b2.addr
       AND c1.hash_value=s1.sql_hash_value
       AND c1.address=s1.sql_address
       AND s2.sql_hash_value = c2.hash_value
       AND l1.block > 0
       AND l2.request > 0
       AND l1.id1 = l2.id1
       AND l1.id2 = l2.id2

#备注

union前的sql可以查看源端执行过的sql(在一个事务里执行了,但是还没提交)。

union后的sql可以查看当前源端正在执行的sql。

一次性展示不全,这里分三个截图展示:

在每条记录里可以看到是谁阻塞了谁。

但是假如源端执行了这个事务后,又执行了其他事务,这时候查看源端阻塞sql就不准确了。

示例:

源端执行了一个事务,然后有执行了一个其他的sql,这时查看源端的阻塞sql是这样的:

这时结果是不准确的。

相关推荐
2401_8576100319 分钟前
实验室管理现代化:Spring Boot技术方案
数据库·spring boot·后端
微微一笑吓死谁28 分钟前
linux系统中oracle数据库数据泵导入dmp数据
linux·数据库·oracle
天草二十六_简村人40 分钟前
Java语言编程,通过阿里云mongo数据库监控实现数据库的连接池优化
java·jvm·数据库·mongodb·阿里云·微服务·云计算
长风清留扬1 小时前
图文解说:MySQL核心模块知识和流程
大数据·数据库·mysql·面试
highfish9202 小时前
Django实现智能问答助手-数据库方式读取问题和答案
数据库·python·django
blammmp2 小时前
MySQL:视图
数据库·mysql
jokerest1232 小时前
web——sqliabs靶场——第十二关——(基于错误的双引号 POST 型字符型变形的注入)
数据库·sql·mybatis
尘浮生2 小时前
Java项目实战II基于微信小程序的私家车位共享系统(开发文档+数据库+源码)
java·开发语言·数据库·学习·微信小程序·小程序·maven
shaoweijava2 小时前
靓车汽车销售网站(源码+数据库+报告)
java·数据库·spring boot·mysql·汽车
weixin_449310842 小时前
如何高效集成YS网店客户和组织映射数据到MongoDB
数据库·mongodb