innodb lock

Innodb type

 

Sharedand Exclusive Locks

IntentionLocks

RecordLocks

GapLocks

Next-KeyLocks

InsertIntention Locks

AUTO-INCLocks

PredicateLocks for Spatial Indexes

 

 

Sharedand Exclusive Locks

 

 

 

 

row-level上 SX鎖 

 

Intention lock

 

Intentionshared (IS): Transaction T intendsto set S locks on individual rows intable t.

 select…lock in share mode 

Intentionexclusive (IX): Transaction T intendsto set X locks on those rows.

 select… for update

Talbe-levellocks

鎖兼容性

 

Record Locks

Recordlock 其實是在索引記錄上加鎖

即使表上沒有顯式的建立索引,innodb會自己建立一個隱藏的clusterindexes

 

Gap Locks

 

 

A gap lock is a lock on a gapbetween index records, or a lock on the gap before the first or after the lastindex record. For example, SELECT c1 FROM t WHERE c1 BETWEEN 10 and 20 FOR UPDATE; prevents other transactionsfrominsertinga value of 15 into column t.c1, whether or nottherewas already any such value in the column, because the gaps between all existingvalues in the range are locked.

gaplock Readcommitted級別是否啓用?

innodb_locks_unsafe_for_binlog 是否啓用gaplock

 

Next-KeyLocks

 

 

Anext-key lock is a combination of a record lock on the index record and a gaplock on the gap before the index record.

左開右閉合區間

 

InsertIntention Locks

 

An insert intention lock is a typeof gap lock set by INSERT operations prior to row insertion.

AUTO-INCLocks

An AUTO-INC lockis a special table-level lock taken by transactions inserting into tableswith AUTO_INCREMENTcolumns.

 

innodb_autoinc_lock_mode  

 

PhantomRows

Theso-called phantom problem occurs within a transaction when the samequery produces different sets of rows at different times.

 

查詢死鎖

Deadlocksin InnoDB

select* from information_schema.innodb_lock_waits;

select* from information_schema.innodb_locks;

Select* from information_schema.innodb_trx;

selectr.trx_idwaiting_trx_id,r.trx_mysql_thread_Idwaiting_thread,b.TRX_STATEholer_state,r.trx_stateblocking_state,

       r.trx_querywaiting_query,b.trx_idblocking_trx_id,

       b.trx_mysql_thread_idblocking_thread,b.trx_queryblocking_query

  from information_schema.innodb_lock_waitsw inner joininformation_schema.innodb_trxb onb.trx_id= w.blocking_trx_id

  inner join information_schema.innodb_trxr onr.trx_id= w.requesting_trx_id;

Q&A

 

select* from table where id=10;

deletefrom t where id=10;

 

 上面語句分別加什麼鎖?

 

前置條件

 

  id列是否爲主鍵

  ②隔離級別是什麼

  id列如果不是主鍵,是否有索引

  id列上如果有二級索引,是否唯一

  SQL對應的執行計劃是什麼

RC隔離級別

id列是主鍵

id列是 uniquesecondary index

id列是nounique secondary index

id列上沒有索引

RR隔離級別

id列是主鍵

id列是 uniquesecondary index

id列是nounique secondary index

id列上沒有索引

 

場景①

 

Id是主鍵, id= 10的記錄加上X鎖即可

 

場景②

 

 

 

Iduniquesecondary index

id列索引對應等於10記錄加X鎖,然後回對應的主鍵索引項(聚簇)X

id列是unique列,其上有unique索引。那麼SQL需要加兩個X鎖,一個對應

idunique索引上的id= 10的記錄,另一把鎖對應於聚簇索引上的[name='d',id=10]的記錄。

 

場景③

 

 

id列上有非唯一索引,那麼對應的所有滿足SQL查詢條件的記錄,都會被加鎖。同時,這些記錄

在主鍵索引上的記錄,也會被加鎖

 

場景④

 

 

id列上沒有索引,SQL會走聚簇索引的全掃描進行過濾。因此每條記錄,無論是否滿足條件,都會被

加上X鎖。但是,爲了效率考量,MySQL做了優化,對於不滿足條件的記錄,會在判斷後放鎖,最終持有

的,是滿足條件的記錄上的鎖,但是不滿足條件的記錄上的加鎖/放鎖動作不會省略。

 

場景⑦

 

 

Repeatable Read隔離級別下,id列上有一個非唯一索引,對應SQLdeletefrom t1 where id = 10;

 首先,通過id索引定位到第一條滿足查詢條件的記錄,加記錄上的X鎖,加GAP上的GAP鎖,然後加主鍵

聚簇索引上的記錄X鎖,然後返回;然後讀取下一條,重複進行。直至進行到第一條不滿足條件的記錄[11,f]

此時,不需要加記錄X鎖,但是仍舊需要加GAP鎖,最後返回結束。

場景⑧

 

RepeatableRead隔離級別下,如果進行全表掃描,那麼會鎖上表中的所有記錄,同時會

鎖上聚簇索引內的所有GAP,杜絕所有的併發更新/刪除/插入 操作

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章