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是这样的:

这时结果是不准确的。

相关推荐
装不满的克莱因瓶1 分钟前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
梦想平凡2 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
TianyaOAO2 小时前
mysql的事务控制和数据库的备份和恢复
数据库·mysql
Ewen Seong2 小时前
mysql系列5—Innodb的缓存
数据库·mysql·缓存
码农老起3 小时前
企业如何通过TDSQL实现高效数据库迁移与性能优化
数据库·性能优化
夏木~4 小时前
Oracle 中什么情况下 可以使用 EXISTS 替代 IN 提高查询效率
数据库·oracle
W21554 小时前
Liunx下MySQL:表的约束
数据库·mysql
黄名富4 小时前
Redis 附加功能(二)— 自动过期、流水线与事务及Lua脚本
java·数据库·redis·lua
言、雲4 小时前
从tryLock()源码来出发,解析Redisson的重试机制和看门狗机制
java·开发语言·数据库
一个程序员_zhangzhen5 小时前
sqlserver新建用户并分配对视图的只读权限
数据库·sqlserver