對Oracle 並行的誤解2--其實會鎖全表,oracle2--

來源:互聯網
上載者:User

對Oracle 並行的誤解2--其實會鎖全表,oracle2--

  以前寫過一篇對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


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:不阻塞

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.