innodb的record lock, row lock, gap lock, next-key lock

record lock – 对索引上某个记录的锁

row lock -对某条数据记录的锁。然而innodb总是通过施加record lock来施加row lock, 也就是说innodb本身没有row lock的概念,record lock就是row lock

gap lock – 索引record之间的锁,用来防止phantom read

next-key lock – 本身不是一种锁。它是a combination of a record lock on the index record and a gap lock on the gap before the index record. 

用next-key lock可以防止phantom read.

对于这样一条语句,

SELECT * FROM child WHERE id > 100 FOR UPDATE;  — 对id=100这条记录上了锁

假设id = {…  90, 100, 105…}

那么当执行这条语句时,其他语句不得插入id=101, id=102…id=103的语句。

可以显式地关掉gap lock.  另外如果isolation level是READ COMMITTED, 则gap lock已经关闭。

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.