MySQL 在查询中使用 IGNORE 时是否仍然会出现死锁错误?
Does MySQL still issues deadlock errors when using IGNORE in a query?
我之前在 mysql
查询中使用过 IGNORE
,我通常使用它来避免 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
关键字只忽略重复键错误,这些具有重复键的行将不会被插入,但其他没有错误的行会插入。将生成警告。
在 concurrent INSERT
的情况下,如果其他线程锁定了 "unique values" 的集合,那么您的线程将等待锁释放并提供deadlock/lock 等待超时错误。
在这种情况下,将不会插入(该语句的)行并返回 ERROR
有table:
CREATE TABLE `t` (
`id` INT(11) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB;
现在 运行 同时查询这 2 个:
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
在您尝试插入 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
关键字只忽略重复键错误,这些具有重复键的行将不会被插入,但其他没有错误的行会插入。将生成警告。
在 concurrent INSERT
的情况下,如果其他线程锁定了 "unique values" 的集合,那么您的线程将等待锁释放并提供deadlock/lock 等待超时错误。
在这种情况下,将不会插入(该语句的)行并返回 ERROR
有table:
CREATE TABLE `t` (
`id` INT(11) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB;
现在 运行 同时查询这 2 个:
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. */