Oracle阻塞(blockingblocked)實(shí)例詳解
一、概述:
阻塞是DBA經(jīng)常碰到的情形,尤其是不良的應(yīng)用程序設(shè)計(jì)所造成的阻塞將導(dǎo)致數(shù)據(jù)庫(kù)性能的嚴(yán)重下降,直至數(shù)據(jù)庫(kù)崩潰。對(duì)DBA而言,有必要知道如何定位到當(dāng)前系統(tǒng)有哪些阻塞,到底誰(shuí)是阻塞者,誰(shuí)是被阻塞者。本文對(duì)此給出了描述并做了相關(guān)演示。
二、演示阻塞:
--更新表,注,提示符scott@CNMMBO表明用戶為scott的session,用戶名不同,session不同。
scott@CNMMBO> update emp set sal=sal*1.1 where empno=7788;
1 row updated.
scott@CNMMBO> @my_env
SPID SID SERIAL# USERNAME PROGRAM
------------ ---------- ---------- --------------- ------------------------------------------------
11205 1073 4642 robin oracle@SZDB (TNS V1-V3)
--另起兩個(gè)session更新同樣的行,這兩個(gè)session都會(huì)處于等待,直到第一個(gè)session提交或回滾
leshami@CNMMBO> update scott.emp set sal=sal+100 where empno=7788;
goex_admin@CNMMBO> update scott.emp set sal=sal-50 where empno=7788;
--下面在第一個(gè)session 查詢阻塞情況
scott@CNMMBO> @blocker
BLOCK_MSG BLOCK
-------------------------------------------------- ----------
pts/5 ('1073,4642') is blocking 1067,10438 1
pts/5 ('1073,4642') is blocking 1065,4464 1
--上面的結(jié)果表明session 1073,4642 阻塞了后面的2個(gè)
--即session 1073,4642是阻塞者,后面2個(gè)session是被阻塞者
--Author : Leshami
--Blog : http://blog.csdn.net/leshami
--下面查詢正在阻塞的session id,SQL語(yǔ)句以及被阻塞的時(shí)間
scott@CNMMBO> @blocking_session_detail.sql
'SID='||A.SID||'WAITCLASS='||A.WAIT_CLASS||'TIME='||A.SECONDS_IN_WAIT||CHR(10)||'QUERY='||B.SQL_TEXT
------------------------------------------------------------------------
sid=1067 Wait Class=Application Time=5995
Query=update scott.emp set sal=sal+100 where empno=7788
sid=1065 Wait Class=Application Time=225
Query=update scott.emp set sal=sal-50 where empno=7788
--下面的查詢阻塞時(shí)鎖的持有情況
scott@CNMMBO> @request_lock_type
USERNAME SID TY LMODE REQUEST ID1 ID2
------------------------------ ---------- -- ----------- ----------- ---------- ----------
SCOTT 1073 TX Exclusive None 524319 27412
LESHAMI 1067 TX None Exclusive 524319 27412
GOEX_ADMIN 1065 TX None Exclusive 524319 27412
--可以看到LESHAMI,GOEX_ADMIN 2個(gè)用戶都在請(qǐng)求524319/27412上的Exclusive鎖,而此時(shí)已經(jīng)被SCOTT加了Exclusive鎖
--查詢阻塞時(shí)鎖的持有詳細(xì)信息
scott@CNMMBO> @request_lock_detail
SID USERNAME OSUSER TERMINAL OBJECT_NAME TY Lock Mode Req_Mode
---------- -------------------- --------------- ------------------------- -------------------- -- ----------- --------------------
1065 GOEX_ADMIN robin pts/1 EMP TM Row Excl
1065 GOEX_ADMIN robin pts/1 Trans-524319 TX --Waiting-- Exclusive
1067 LESHAMI robin pts/0 EMP TM Row Excl
1067 LESHAMI robin pts/0 Trans-524319 TX --Waiting-- Exclusive
1073 SCOTT robin pts/5 EMP TM Row Excl
1073 SCOTT robin pts/5 Trans-524319 TX Exclusive
三、文中涉及到的相關(guān)SQL腳本完整代碼如下:
robin@SZDB:~/dba_scripts/custom/sql> more my_env.sql
SELECT spid, s.sid, s.serial#, p.username, p.program
FROM v$process p, v$session s
WHERE p.addr = s.paddr
AND s.sid = (SELECT sid
FROM v$mystat
WHERE rownum = 1);
robin@SZDB:~/dba_scripts/custom/sql> more blocker.sql
col block_msg format a50;
select c.terminal||' ('''||a.sid||','||c.serial#||''') is blocking '||b.sid||','||d.serial# block_msg, a.block
from v$lock a,v$lock b,v$session c,v$session d
where a.id1=b.id1
and a.id2=b.id2
and a.block>0
and a.sid <>b.sid
and a.sid=c.sid
and b.sid=d.SID;
robin@SZDB:~/dba_scripts/custom/sql> more blocking_session_detail.sql
--To find the query for blocking session
--Access Privileges: SELECT on v$session, v$sqlarea
SELECT 'sid='
|| a.SID
|| ' Wait Class='
|| a.wait_class
|| ' Time='
|| a.seconds_in_wait
|| CHR (10)
|| ' Query='
|| b.sql_text
FROM v$session a, v$sqlarea b
WHERE a.blocking_session IS NOT NULL AND a.sql_address = b.address
ORDER BY a.blocking_session
/
robin@SZDB:~/dba_scripts/custom/sql> more request_lock_type.sql
--This script generates a report of users waiting for locks.
--Access Privileges: SELECT on v$session, v$lock
SELECT sn.username, m.sid, m.type,
DECODE(m.lmode, 0, 'None',
1, 'Null',
2, 'Row Share',
3, 'Row Excl.',
4, 'Share',
5, 'S/Row Excl.',
6, 'Exclusive',
lmode, ltrim(to_char(lmode,'990'))) lmode,
DECODE(m.request,0, 'None',
1, 'Null',
2, 'Row Share',
3, 'Row Excl.',
4, 'Share',
5, 'S/Row Excl.',
6, 'Exclusive',
request, ltrim(to_char(m.request,
'990'))) request, m.id1, m.id2
FROM v$session sn, v$lock m
WHERE (sn.sid = m.sid AND m.request != 0)
OR (sn.sid = m.sid
AND m.request = 0 AND lmode != 4
AND (id1, id2) IN (SELECT s.id1, s.id2
FROM v$lock s
WHERE request != 0
AND s.id1 = m.id1
AND s.id2 = m.id2)
)
ORDER BY id1, id2, m.request;
robin@SZDB:~/dba_scripts/custom/sql> more request_lock_detail.sql
set linesize 190
col osuser format a15
col username format a20 wrap
col object_name format a20 wrap
col terminal format a25 wrap
col Req_Mode format a20
select B.SID, C.USERNAME, C.OSUSER, C.TERMINAL,
DECODE(B.ID2, 0, A.OBJECT_NAME,
'Trans-'||to_char(B.ID1)) OBJECT_NAME,
B.TYPE,
DECODE(B.LMODE,0,'--Waiting--',
1,'Null',
2,'Row Share',
3,'Row Excl',
4,'Share',
5,'Sha Row Exc',
6,'Exclusive',
'Other') "Lock Mode",
DECODE(B.REQUEST,0,' ',
1,'Null',
2,'Row Share',
3,'Row Excl',
4,'Share',
5,'Sha Row Exc',
6,'Exclusive',
'Other') "Req_Mode"
from DBA_OBJECTS A, V$LOCK B, V$SESSION C
where A.OBJECT_ID(+) = B.ID1
and B.SID = C.SID
and C.USERNAME is not null
order by B.SID, B.ID2;
相關(guān)文章
Oracle數(shù)據(jù)庫(kù)中創(chuàng)建自增主鍵的實(shí)例教程
Oracle的字段自增功能,可以利用創(chuàng)建觸發(fā)器的方式來(lái)實(shí)現(xiàn),接下來(lái)我們就來(lái)看看Oracle數(shù)據(jù)庫(kù)中創(chuàng)建自增主鍵的實(shí)例教程,需要的朋友可以參考下2016-05-05
oracle如何合并多個(gè)sys_refcursor詳解
這篇文章主要給大家介紹了關(guān)于oracle如何合并多個(gè)sys_refcursor的相關(guān)資料,以及在文末跟大家分享了sys_refcursor 和 cursor 優(yōu)缺點(diǎn)的比較,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Oracle?VM?VirtualBox?虛擬機(jī)硬盤擴(kuò)容
這篇文章主要介紹了Oracle?VM?VirtualBox?虛擬機(jī)硬盤擴(kuò)容,VirtualBox中使用Ubuntu虛擬機(jī)中,出現(xiàn)了虛擬硬盤不夠用的情況,我們可以采取下文方式進(jìn)行擴(kuò)容,需要的朋友可以參考一下2022-03-03
oracle數(shù)據(jù)庫(kù)中l(wèi)istagg函數(shù)使用詳解
listagg函數(shù)是Oracle數(shù)據(jù)庫(kù)中的一個(gè)聚合函數(shù),用于將一組值連接成一個(gè)以指定分隔符分隔的字符串,這篇文章主要給大家介紹了關(guān)于oracle數(shù)據(jù)庫(kù)中l(wèi)istagg函數(shù)使用的相關(guān)資料,需要的朋友可以參考下2024-06-06
修改計(jì)算機(jī)名或IP后Oracle10g服務(wù)無(wú)法啟動(dòng)的解決方法
修改計(jì)算機(jī)名或IP后Oracle10g無(wú)法啟動(dòng)服務(wù)即windows服務(wù)中有一項(xiàng)oracle服務(wù)啟動(dòng)不了,報(bào)錯(cuò),下面是具體的解決方法2014-01-01
Oracle數(shù)據(jù)更改后出錯(cuò)的解決方法
這篇文章主要介紹了Oracle數(shù)據(jù)更改后出錯(cuò)的解決方法,需要的朋友可以參考下2014-07-07

