2

I am encountering deadlocks while trying to update multiple rows (a batch) using INSERT..ON DUPLICATE KEY UPDATE. Does MySQL lock all the rows in one batch or does it lock only the row it is updating at a point of time?

2 Answers 2

1

This would depend on the Storage Engine.

  • MyISAM : Table-level locks are performed. Consequently, all rows are locked.

  • InnoDB : Row-level locks are employed. Since InnoDB is transactional, all rows are locked implcitly. Those locks would be visible when you run SHOW ENGINE INNODB STATUS\G. If you want to manually lock those rows beforehand to prevent sharing, you could run SELECT ... FOR UPDATE.

0

since mysql 5.7, it will acquire GAP lock if the value(has unique index) does not exist.

INSERT INTO ... SELECT ... ON DUPLICATE KEY UPDATE and LOAD DATA CONCURRENT REPLACE took too weak a lock, leading to the possibility of concurrent SELECT statements returning inconsistent results. (Bug #38046, Bug #11749055)

refer this link for detail:

https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-0.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.