mysql - SQL INSERT 是否涉及任何读/写锁?

标签 mysql insert locking

这是一个非常简单的问题,但我真的无法在谷歌上找到任何有用的答案。

我一直在读到在 SELECT 上有一个 READ LOCK,在 UPDATE 语句上有一个 WRITE LOCK。但是,我想知道当我们插入表时是否有任何类型的锁?

假设该表使用 InnoDB 作为引擎。

最佳答案

插入时,InnoDB 会创建一个所谓的“间隙锁”。

The manual describes this quite well :

A type of gap lock called an insertion intention gap lock is set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap. Suppose that there are index records with values of 4 and 7. Separate transactions that attempt to insert values of 5 and 6 each lock the gap between 4 and 7 with insert intention locks prior to obtaining the exclusive lock on the inserted row, but do not block each other because the rows are nonconflicting

除此之外,任何唯一索引都将针对提供的值进行锁定,以确保两个不同的事务不会插入相同的值(这与间隙锁略有不同,如果我'我没记错)。

关于mysql - SQL INSERT 是否涉及任何读/写锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15084171/

相关文章:

mysql - 仅启用 createdAt 时间戳并忽略 Sequelize 中的 updatedAt 时间戳

c++ - boost::scoped_lock 不适用于局部静态变量?

java - 可以从一个线程转移到另一个线程的锁

python - Python 脚本可以在运行时在内存中识别自己吗?

php - 返回 PDO 数据

php - 您将如何构造此 php - mysql 查询?

php - 如何使用 Codeigniter 将数组插入 MySQL?

php - 从另一个表和变量中插入值

php - 为什么我的 MySQL INSERT 语句返回错误? PDOException SQLSTATE[42000] :

java - SQL Server : Row doesn't appear after insert