我用过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/