關於鎖的快速定位

Steven1981發表於2008-05-23

當我們在執行某個update,或delete的時候,釋出自己的SQL等在那裡,如何更快速地定位到問題源?

[@more@]

--------檢視哪些物件被鎖了!

select t2.username,
t2.sid,
t2.serial#,
t3.object_name,
t2.OSUSER,
t2.MACHINE,
t2.PROGRAM
from v$locked_object t1, v$session t2, dba_objects t3
where t1.session_id = t2.sid
and t1.object_id = t3.object_id
order by t2.logon_time;

--------察看錶鎖(可以檢視什麼型別的鎖)

select /*+ rule */
s.sid sid,s.username username,s.machine machine,
l.type type,o.object_name object_name,
DECODE(l.lmode,
0,'None',
1,'Null',
2,'Row Share',
3,'Row Exlusive',
4,'Share',
5,'Sh/Row Exlusive',
6,'Exclusive') lmode,
DECODE(l.request,
0,'None',
1,'Null',
2,'Row Share',
3,'Row Exlusive',
4,'Share',
5,'Sh/Row Exlusive',
6,'Exclusive') request, l.block block
from v$lock l,v$session s, dba_objects o
where l.sid = s.sid and username != 'SYSTEM' and o.object_id(+) = l.id1;


------由於上面的語句很多,又比較難記。
其實當有等等的情況發生的時候,我們可以用以上檢視來檢視:
有誰在等待?
在等等哪個SESSION?

select * from dba_blockers;

--HOLDING_SESSION就是SESSION_ID

select * from dba_waiters;

--HOLDING_SESSION就是被等待的SESSION_ID
--WAITING_SESSION就是等待的SESSION_ID

從上面兩種方法,其實我們已經能基本定位哪個SESSION鎖了表。
下面來看一下,那個SESSION到底在執行哪些SQL?

SELECT /*+ ORDERED */
sql_text
FROM v$sqltext a
WHERE (a.hash_value, a.address) IN
(SELECT DECODE(sql_hash_value, 0, prev_hash_value, sql_hash_value),
DECODE(sql_hash_value, 0, prev_sql_addr, sql_address)
FROM v$session b
WHERE b.sid = &SID_ID)
ORDER BY piece ASC

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

相關文章