v$lock_处理TX锁实验及总结

【Oracle九大性能视图】之1.v$lock_处理TX锁实验及总结

Oracle 2011-04-15 10:55:01 阅读57 评论0   字号: 订阅

 
转自:http://space.itpub.net/?uid-9664900-action-viewspace-itemid-676758

This view lists the locks currently held by the Oracle Database and outstanding requests for a lock or latch.

Column Datatype Description
ADDR RAW(4 | 8) Address of lock state object
KADDR RAW(4 | 8) Address of lock
SID NUMBER Identifier for session holding or acquiring the lock
TYPE VARCHAR2(2) Type of user or system lock

The locks on the user types are obtained by user applications. Any process that is blocking others is likely to be holding one of these locks. The user type locks are:

TM - DML enqueue

TX - Transaction enqueue

UL - User supplied

The locks on the system types are held for extremely short periods of time. The system type locks are listed in Table 6-1.

ID1 NUMBER Lock identifier #1 (depends on type)
ID2 NUMBER Lock identifier #2 (depends on type)
LMODE NUMBER Lock mode in which the session holds the lock:
  • 0 - none

  • 1 - null (NULL)

  • 2 - row-S (SS)

  • 3 - row-X (SX)

  • 4 - share (S)

  • 5 - S/Row-X (SSX)

  • 6 - exclusive (X)

REQUEST NUMBER Lock mode in which the process requests the lock:
  • 0 - none

  • 1 - null (NULL)

  • 2 - row-S (SS)

  • 3 - row-X (SX)

  • 4 - share (S)

  • 5 - S/Row-X (SSX)

  • 6 - exclusive (X)

CTIME NUMBER Time since current mode was granted
BLOCK NUMBER A value of either 0 or 1, depending on whether or not the lock in question is the blocker.

 

Table 6-1 Values for the TYPE Column: System Types

System Type Description System Type Description

BL

Buffer hash table instance

NA..NZ

Library cache pin instance (A..Z = namespace)

CF

Control file schema global enqueue

PF

Password File

CI

Cross-instance function invocation instance

PI, PS

Parallel operation

CU

Cursor bind

PR

Process startup

DF

datafile instance

QA..QZ

Row cache instance (A..Z = cache)

DL

Direct loader parallel index create

RT

Redo thread global enqueue

DM

Mount/startup db primary/secondary instance

SC

System change number instance

DR

Distributed recovery process

SM

SMON

DX

Distributed transaction entry

SN

Sequence number instance

FS

File set

SQ

Sequence number enqueue

HW

Space management operations on a specific segment

SS

Sort segment

IN

Instance number

ST

Space transaction enqueue

IR

Instance recovery serialization global enqueue

SV

Sequence number value

IS

Instance state

TA

Generic enqueue

IV

Library cache invalidation instance

TS

Temporary segment enqueue (ID2=0)

JQ

Job queue

TS

New block allocation enqueue (ID2=1)

KK

Thread kick

TT

Temporary table enqueue

LA .. LP

Library cache lock instance lock (A..P = namespace)

UN

User name

MM

Mount definition global enqueue

US

Undo segment DDL

MR

Media recovery

WL

Being-written redo log instance

 

锁的分类:
k?c7Y S:]aD1959710、无ITPUB个人空间#H'L#^E]]&V `)A
1、NULL,可以某些情况下,如分布式数据库的查询会产生此锁。ITPUB个人空间9FT$m|?nKZ
2、SS,表结构共享锁
T)rK9e-?d2o]1959713、SX,表结构共享锁+被操作的记录的排它锁ITPUB个人空间u0x Cb(]
4、S, 表结构共享锁+所有记录共享锁ITPUB个人空间n,b-m7Qc%q5B
5、SRX 表结构共享锁+所有记录排它锁
W-ul'D{aXCZoC1959716、X   表结构排它锁+所有记录排它锁
/HMcBB195971表上的5种锁的含义:
:A,ig$RF9YN195971RS(SS):是一种纯粹的意向锁,它表征事务要通过SELECT FOR UPDATE访问某些行;ITPUB个人空间)N7Zu3A#w [sK
RX(SX):是一种纯粹的意向锁,它表征事务要通过 UPDATE/ DELETE/INSERT修改某些行;ITPUB个人空间 `0@yB8V[D6I H?J
S:全表范围的共享锁,不需要在每行上做出标识;
&c"CHP,MA-F5D195971X:全表范围的排它锁,不需要在每行上做出标识;
Z J;FBq]+|195971SRX(SSX):是意向锁RX与S锁的组合,它表征事务既对表加S锁,同时要修改个别行,即要在个别行上加X锁;(如果你使用ORACLE8,可以在一个表上建立一个自引用,如EMP表上的MGR引用EMPNO,删除一条记录,可以观察到SRX锁)
'C2j4B1|wa195971在行上,ORALCE只有X锁,ITPUB个人空间]7Uy?*b8`4QL9qQ
但由SELECT FOR UPDATE 获得的X锁在表级表征为RS锁,ITPUB个人空间7m \ Rq!R6^Ec
由INSERT/UPDATE/DELETE 获得的X锁在表级表征为RX锁,ITPUB个人空间](K7r:P'[
 
#@{ SBf Z195971窗口1:
JU,|*jCs6j J195971SQL> conn myfmis/myfmisITPUB个人空间3Z$}^ mx9o#j,k$_
已连接。
ya%K2l\:Ej195971SQL> update xtdwcs set MC='单位本部ok' where dh='0114';

窗口2:
e3C.C;{eP195971SQL> conn myfmis/myfmis
? p&B4] s-|X4p@195971已连接。
k;X/kX&SS195971SQL> update xtdwcs set MC=单位本部ok' where dh='0114';ITPUB个人空间k6ZF/ilJU
处理过程:
Jr u:r9v w;w n195971查看是否有session阻塞:
1v2Z&_"pgy6d"jt195971select * from v$lock where BLOCK=1ITPUB个人空间gzZ asJL
    ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
.]_h4X0g]~\ |H1959711 1FE0C654 1FE0C770 151 TX 131115 520 6 0 669 1ITPUB个人空间.{ w4a$mf7Ek_B+p
查看TX锁的SID与Serial#:ITPUB个人空间8N!Y E4@?t(nSw*u[
select * from v$session where event like'%TX%';ITPUB个人空间7G2pR'wD
select sid,serial#,username,state,blocking_session_status,blocking_session from v$session where event like'%TX%';ITPUB个人空间g$Y xt]~$R5FAlM
 
Zb%Z/]F U195971    SID SERIAL# USERNAME STATE BLOCKING_SESSION_STATUS BLOCKING_SESSIONITPUB个人空间{S)B1\7_,G)E
1 137 6 FMISMAIN WAITING VALID 150
CAR.aq'Hq195971临时KILL这个session:ITPUB个人空间d7J'l?rt&?-T
分两类,
:kg9_8FN4n p195971第一:kill掉137,6,则是kill掉之后被等的session。ITPUB个人空间'_t?y P:E ]
alter system kill session '137,6';ITPUB个人空间Ns2Cz a8z:CSE&v
第二:kill掉150,则是kill掉之前阻塞别人的session。ITPUB个人空间Z:\*TxzM
因此通过前面的137,6所查到的SQL语句,则是后面运行被等待的SQL语句,而不是阻塞别人的SQL语句。ITPUB个人空间z9@J tB(d?l
如果要查找原因,操作如下:
A%uT7n;C&D&L2K[c195971查找相应的SQL语句:
Wp.y ]c'bQ195971select a.sql_text from v$sqlarea a,v$session b where a.sql_id=b.sql_id
;Cc%{Bvm195971select a.sql_text from v$sqlarea a,v$session b where a.sql_id=b.sql_id and b.event like'%TX%'
b.sAp X4p;hQ{4{}F)x195971 
/^_,Mq@G Rc5n195971    SQL_TEXT
g?A!b|0G(U*}CX/Y8y1959711 update xtdwcs set MC='单位本部ok'  where dh='0114'ITPUB个人空间E!xM/N c0Hb9q
select * from v$sqlarea where SQL_ID='gf1472pj5wnzj'ITPUB个人空间J?aE u.?CF$Z
找出相应的SQL语句后,即可处理。ITPUB个人空间't+`@*{R.K0Hj
#######################################################################
;Y(m?I/f`p Z'~195971在巡检期间,检查如果发现有相关的session_wait锁,可按以下步骤查找相应的锁
Q?u1e+ti.o1959711. 使用V$LOCK找出session持有的锁。ITPUB个人空间J?H)XK#K L'Y)r#\_
2. 使用V$SESSION找出持有锁或等待锁的session执行的sql语句。
&D.iH3{"`E2j _ Xu+Km1959713. 使用V$SESSION_WAIT找出什么原因导致session持有锁堵塞。
+O!AJ1I2F&Kw*U1959714. 使用V$SESSION获取关于持有锁的程序和用户的更多信息。ITPUB个人空间E Hm,}2]8g aT?g7Q"U
V$LOCK中的常用列
%@v$G+Y2P[*C,iA195971? SID:表示持有锁的会话信息。ITPUB个人空间E*M^5x7O&s
? TYPE:表示锁的类型。值包括TM和TX等。
!cy{+u(lt qV195971? LMODE:表示会话等待的锁模式的信息。用数字0-6表示,和表1相对应。ITPUB个人空间4NVw/Q$hJ
? REQUEST:表示session请求的锁模式的信息。ITPUB个人空间;?\1A%A^
? ID1,ID2:表示锁的对象标识。

1、查看相应的v$session_wait信息ITPUB个人空间&[;z)C k0Q w
    select event,count(*) from V$SESSION_WAIT group by event order by count(*) desc;
dxO!j|.m{*uL0jpA+l195971    select event,count(*) from V$SESSION_WAIT group by event order by count(*) asc;ITPUB个人空间A,F@8z7{\B
如果如下(第3条):ITPUB个人空间R Fp5hHckI[h
    EVENT COUNT(*)ITPUB个人空间~f#g+R$o7Zbk
1 smon timer 1
~k$H7U2r0o1959712 Streams AQ: qmn coordinator idle wait 1
;Ob(LH2] U4C9A^Hik1959713 enq: TX - row lock contention 1
$Iw?ONA1959714 jobq slave wait 1
yUFIo(@3D-w1959715 Streams AQ: waiting for time management or cleanup tasks 1ITPUB个人空间9l0k Kl `U
6 pmon timer 1ITPUB个人空间;gmzY0w6[p/g
7 Streams AQ: qmn slave idle wait 1
&C!f&N0Dhk,q8J1959718 SQL*Net message from client 3ITPUB个人空间r |3PX7iA
9 rdbms ipc message 9ITPUB个人空间)]$s`\PV
 
)MoDsH-J1S7h*w H195971查看当前被锁的session正在执行的sql语句
-{BP6O5K$G B195971select /*+ NO_MERGE(a) NO_MERGE(b) NO_MERGE(c) */ a.username, a.machine, a.sid, a.serial#, a.last_call_et "Seconds", b.id1, c.sql_text "SQL"ITPUB个人空间k#jXlHsL%g
from v$session a, v$lock b, v$sqltext cITPUB个人空间n!mD+zoICs
where a.username is not null and a.lockwait = b.kaddr and c.hash_value =a.sql_hash_value

转载请请注明来处

你可能感兴趣的:(oracle,职场,锁,休闲)