how to avoid deadlock in mysql

后端 未结 2 1223
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 01:58

I have the following query (all tables are innoDB)

INSERT INTO busy_machines(machine) 
               SELECT machine FROM all_machines 
               WHERE mach         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 02:26

    To my understanding, a select does not acquire lock and should not be the cause of the deadlock.

    Each time you insert/update/or delete a row, a lock is acquired. To avoid deadlock, you must then make sure that concurrent transactions don't update row in an order that could result in a deadlock. Generally speaking, to avoid deadlock you must acquire lock always in the same order even in different transaction (e.g. always table A first, then table B).

    But if within one transaction you insert in only one table this condition is met, and this should then usually not lead to a deadlock. Are you doing something else in the transaction?

    A deadlock can however happen if there are missing indexes. When a row in inserted/update/delete, the database need to check the relational constraints, that is, make sure the relations are consistent. To do so, the database needs to check the foreign keys in the related tables. It might result in other lock being acquired than the row that is modified. Be sure then to always have index on the foreign keys (and of course primary keys), otherwise it could result in a table lock instead of a row lock. If table lock happen, the lock contention is higher and the likelihood of deadlock increases.

    Not sure what happens exactly in your case, but maybe it helps.

提交回复
热议问题