一條查詢Oracle中的阻塞鎖(以及阻塞在哪個資料上)的SQL

season0891發表於2014-04-17

Select '節點 ' || a.INST_ID || ' session ' || a.sid || ',' || a_s.SERIAL# ||
       ' 阻塞了 節點 ' || b.INST_ID || ' session ' || b.SID || ',' || b_s.SERIAL# blockinfo,
       a.INST_ID,
       a_s.SID,
       a_s.SCHEMANAME,
       a_s.MODULE,
       a_s.STATUS,
       a.type lock_type,
       a.id1,
       a.id2,
       decode(a.lmode,
              0,
              'none',
              1,
              null,
              2,
              'row-S (SS)',
              3,
              'row-X (SX)',
              4,
              'share (S)',
              5,
              'S/Row-X (SSX)',
              6,
              'exclusive (X)') lock_mode,
      
       '後為被阻塞資訊' ,
       b.INST_ID blocked_inst_id,
       b_s.SID blocked_sid,
       b.TYPE blocked_lock_type,
       decode(b.request,
              0,
              'none',
              1,
              null,
              2,
              'row-S (SS)',
              3,
              'row-X (SX)',
              4,
              'share (S)',
              5,
              'S/Row-X (SSX)',
              6,
              'exclusive (X)') blocked_lock_request,
       b_s.SCHEMANAME blocked_SCHEMANAME,
       b_s.MODULE blocked_module,
       b_s.STATUS blocked_status,
       b_s.SQL_ID blocked_sql_id,
       obj.owner blocked_owner,
       obj.object_name blocked_object_name,
       obj.OBJECT_TYPE blocked_OBJECT_TYPE,
       case
          when b_s.ROW_WAIT_OBJ# <> -1 then
             dbms_rowid.rowid_create(1,
                                     obj.DATA_OBJECT_ID,
                                     b_s.ROW_WAIT_FILE#,
                                     b_s.ROW_WAIT_BLOCK#,
                                     b_s.ROW_WAIT_ROW#)
          else
            '-1'
          end blocked_rowid, --被阻塞資料的rowid
       decode(obj.object_type,
              'TABLE',
              'select * from ' || obj.owner || '.' || obj.object_name ||
              ' where rowid=''' ||
              dbms_rowid.rowid_create(1,
                                      obj.DATA_OBJECT_ID,
                                      b_s.ROW_WAIT_FILE#,
                                      b_s.ROW_WAIT_BLOCK#,
                                      b_s.ROW_WAIT_ROW#) || '''',
              NULL)  blocked_data_querysql

  from gv$lock     a,
       gv$lock     b,
       gv$session  a_s,
       gv$session  b_s,
       dba_objects obj
 where a.id1 = b.id1
   and a.id2 = b.id2
   and a.BLOCK > 0 --阻塞了其他人
   and b.request > 0
   and ((a.INST_ID = b.INST_ID and a.sid <> b.sid) or
       (a.INST_ID <> b.INST_ID))
   and a.sid = a_s.sid
   and a.INST_ID = a_s.INST_ID
   and b.sid = b_s.sid
   and b.INST_ID = b_s.INST_ID
   and b_s.ROW_WAIT_OBJ# = obj.object_id(+)
 order by a.inst_id,a.sid

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-1144203/,如需轉載,請註明出處,否則將追究法律責任。

相關文章