How to debug Lock wait timeout exceeded on MySQL?

前端 未结 11 953
我寻月下人不归
我寻月下人不归 2020-11-22 11:46

In my production error logs I occasionally see:

SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

11条回答
  •  抹茶落季
    2020-11-22 12:09

    Here is what I ultimately had to do to figure out what "other query" caused the lock timeout problem. In the application code, we track all pending database calls on a separate thread dedicated to this task. If any DB call takes longer than N-seconds (for us it's 30 seconds) we log:

    -- Pending InnoDB transactions
    SELECT * FROM information_schema.innodb_trx ORDER BY trx_started; 
    
    -- Optionally, log what transaction holds what locks
    SELECT * FROM information_schema.innodb_locks;
    

    With above, we were able to pinpoint concurrent queries that locked the rows causing the deadlock. In my case, they were statements like INSERT ... SELECT which unlike plain SELECTs lock the underlying rows. You can then reorganize the code or use a different transaction isolation like read uncommitted.

    Good luck!

提交回复
热议问题