mysql - 在查询中使用 IGNORE 时,MySQL 是否仍然会出现死锁错误?

标签 mysql sql database-deadlocks

我用过IGNORE之前在 mysql查询,我通常使用它来避免 mysql当您尝试插入 duplicate key 时返回错误,但是我不确定这是如何工作的 - mysql 在使用 IGNORE 时是否只是忽略错误或者它只忽略 duplicate key错误?

例如,如果 deadlock使用 IGNORE 时是否仍然会出现错误?在查询中?

编辑:

做了更多阅读并发现了以下信息:

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are ignored. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row is discarded and no error occurs. Ignored errors may generate warnings instead, although duplicate-key errors do not.

好吧,我猜这意味着死锁错误只会生成警告?

最佳答案

IGNORE 关键字仅忽略重复键错误,并且不会插入具有重复键的这些行,但不会插入其他没有错误的行。将生成警告。

并发INSERT的情况下,如果其他线程锁定了一组“唯一值”,那么您的线程将等待锁释放并产生死锁/锁定等待超时超出错误。 在这种情况下,不会插入(该语句的)行并返回 ERROR

有表:

CREATE TABLE `t` (
    `id` INT(11) NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE=InnoDB;

现在同时运行这两个查询:

INSERT IGNORE INTO t(id)
VALUES (5+sleep(1)),(6+sleep(1)),(3+sleep(1)),(4+sleep(1));
/* Affected rows: 4  Found rows: 0  Warnings: 0  Duration for 1 query: 4,586 sec. */

INSERT IGNORE INTO t(id)
VALUES (3+sleep(1)),(4+sleep(1)),(5+sleep(1)),(6+sleep(1));
/* SQL Error (1213): Deadlock found when trying to get lock; try restarting transaction */
/* Affected rows: 0  Found rows: 0  Warnings: 0  Duration for 0 of 1 query: 0,000 sec. */

关于mysql - 在查询中使用 IGNORE 时,MySQL 是否仍然会出现死锁错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676071/

相关文章:

mysql - 在mysql中使用事务插入多个表

mysql - 远程连接到 clearDB heroku 数据库

postgresql - 依靠 Postgres 的死锁检测来进行并发控制是否安全?

php - 如何使用 PHPUnit 对并发读/写进行单元测试?

mysql 合并 2 个表,结构相同,键值重叠(auto_increment)

mysql - 在不编辑 mysql 查询的情况下处理丢失的列

sql - 在 PostgreSQL 中按字母顺序搜索 friend

java - 为什么 Oracle Pivot 产生不存在的结果?

sql - MySql中utf8_general_ci和utf8_unicode_ci有什么区别?

postgresql - 运行更新时 PostgreSQL 中的死锁