mysql locking

1. 意向锁

https://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html#innodb-insert-intention-locks

官方文档,意向锁是Innodb为了支持多种粒度锁(表锁+行锁)设计的。

举例:

一个表 tab1

id, name

2, "xxxx"

id是unique key。

事务T1 select ... from tab1 where id=2 for update.

事务T2 lock tab1 .... write。

若无意向锁,事务T2需扫描表的每一行,看下是否有锁。

有了意向锁后,T2只需判断与T1的意向锁是否兼容即可。

2. Gap锁

Gap locks in InnoDB are purely inhibitive”, which means that their only purpose is to prevent other transactions from inserting to the gap. Gap locks can co-exist. A gap lock taken by one transaction does not prevent another transaction from taking a gap lock on the same gap. There is no difference between shared and exclusive gap locks. They do not conflict with each other, and they perform the same function.

3. Next-Key Locks

是Gap锁 与 Record Locks的结合,区间为前开后闭。如一个index 有10, 11, 13, and 20几条记录,则可能存在的Next-Key Locks是:

(negative infinity, 10]

(10, 11]

(11, 13]

(13, 20]

(20, positive infinity)

Next-Key Locks用于RR事务隔离级别,解决幻读问题。

猜你喜欢

转载自www.cnblogs.com/gm-201705/p/10057580.html