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,杜绝所有的并发更新/删除/插入 操作

猜你喜欢

转载自blog.csdn.net/jing_flower/article/details/79209293