以前写过一篇对Oracle 并行的误解1--其实你没有用到并行,我一直以为Oracle只会锁住需要修改的数据,但在并行下是这样的,并行update是会锁全表的,下面的实验:
先做一个修改的实验:
session1:
SQL> select sid from v$mystat where rownum=1;SID
----------
11
SQL> select sid from v$mystat where rownum=1;
SQL> create table test as select * from dba_objects;
SQL> alter session force parallel dml;
SQL> update /*+parallel(t,4)*/ test t set object_name='ggg' where object_type='TABLE';
session2:
SQL> select sid from v$mystat where rownum=1;
SID
----------
8
SQL> update test t set object_name='ggg' where object_type='INDEX';--hang住
session3:
SQL> select sid,type,id1,id2,lmode,request,ctime,block from v$lock where sid in(8,11) order by sid;
SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ---------- ----------
8 TM 80189 0 0 3 61 0
8 AE 100 0 4 0 1871 0
11 TO 65927 1 3 0 1305 0
11 TX 196620 5212 6 0 64 0
11 PS 1 2 4 0 64 0
11 PS 1 3 4 0 64 0
11 TM 80189 0 6 0 64 1
11 PS 1 0 4 0 64 0
11 AE 100 0 4 0 2836 0
11 PS 1 1 4 0 64 0
SQL> select object_id,locked_mode from v$locked_object where session_id in(8,11);
OBJECT_ID LOCKED_MODE
---------- -----------
80189 6
80189 0
SQL> select sid,event from v$session_wait where wait_class <>'Idle' and sid in(8,11);
SID EVENT
---------- -------------------------------------------------------------------------
8 enq: TM - contention
再做一个新增的实验:
create table test(
id number,
name varchar2(50)
);
session1:
alter session force parallel dml;
insert /*+parallel(4)*/ into test select rownum,'aa'||rownum from dual connect by level < 100000;
session2:
insert into test t values(0,'aa1');--hang住
session3:
SQL> select sid,event from v$session_wait where wait_class<>'Idle' ;
SID EVENT
---------- ----------------------------
9 enq: TM - contention
V$LOCK视图结构
TYPE
区分该锁保护对象的类型(表4)
TM – DML enqueue
TX – Transaction enqueue
UL – User supplied
–我们主要关注TX和TM两种类型的锁
–UL锁用户自己定义的,一般很少会定义,基本不用关注
–其它均为系统锁,会很快自动释放,不用关注
ID1
ID2
ID1,ID2的取值含义根据type的取值而有所不同
对于TM锁
ID1表示被锁定表的object_id 可以和dba_objects视图关联取得具体表信息,ID2 值为0
对于TX?锁
ID1以十进制数值表示该事务所占用的回滚段号和事务槽slot number号,其组形式:
0xRRRRSSSS,RRRR=RBS/UNDO NUMBER,SSSS=SLOT NUMBER
ID2 以十进制数值表示环绕wrap的次数,即事务槽被重用的次数
LMODE
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
同LMODE,大于0时,表示当前会话被阻塞,其它会话占有改锁的模式
BLOCK
是否阻塞其他会话锁申请 1:阻塞 0:不阻塞