1. 程式人生 > >Oracle RAC阻塞排查SQL指令碼

Oracle RAC阻塞排查SQL指令碼

 

該使用到的sql指令碼是參考別人技術文件轉接過來用於日常工作中使用到的記錄下。

1,樹形結構分級別顯示會話之間的阻塞關係

set lines 200 pages 100
col tree for a30
col event for a40
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(a.SID||'@'||a.inst_id, ' <- ') tree,
               level as tree_level
          from gv$session a
         start with a.blocking_session is not null
        connect by (a.sid||'@'||a.inst_id) = prior (a.blocking_session||'@'||a.blocking_instance))
 where isleaf = 1
 order by tree_level asc;

效果如下:

2,直接顯示會話阻塞關係

SELECT DISTINCT  
          s1.username  
       || '@'  
       || s1.machine  
       || ' ( INST='  
       || s1.inst_id  
       || ' SID='  
       || s1.sid  
       || ' Serail#='  
       || s1.serial#  
       || ' ) IS BLOCKING '  
       || s2.username  
       || '@'  
       || s2.machine  
       || ' ( INST='  
       || s2.inst_id  
       || ' SID='  
       || s2.sid  
       || ' Serial#='  
       || s2.serial#  
       || ' ) '  
          AS blocking_status  
  FROM gv$lock l1,  
       gv$session s1,  
       gv$lock l2,  
       gv$session s2  
 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 l1.block > 0  
       AND l2.request > 0  
       AND l1.id1 = l2.id1  
       AND l1.id2 = l2.id2;

效果如下:

3,顯示阻塞例項的sql語句(需要在RAC的每個例項上面去執行)

select b.sid,
       a.sql_id,
       a.sql_text,
       a.hash_value,
       b.username,
       b.machine,
       a.module,
       decode(c.block, 1, 'blocking') blocking,
       decode(c.request, 0, 'null', 'blocked') blocked,
       to_char(b.logon_time, 'yyyy-mm-dd hh24:mi:ss')
  from v$sql a, v$session b, v$lock c
 where c.type = 'TX'
   and a.sql_id = b.sql_id
   and b.sid = c.sid
union all
select b.sid,
       a.sql_id,
       a.sql_text,
       a.hash_value,
       b.username,
       b.machine,
       a.module,
       decode(c.block, 1, 'blocking') blocking,
       decode(c.request, 0, 'null', 'blocked') blocked,
       to_char(b.logon_time, 'yyyy-mm-dd hh24:mi:ss')
  from v$sql a, v$session b, v$lock c
 where c.type = 'TX'
   and a.sql_id = b.prev_sql_id
   and b.sid = c.sid
   and c.block = 1;

效果如下: