Question

We've been receiving some deadlocks and this our latest SHOW ENGINE INNODB STATUS

Query 1:

UPDATE actions SET
    hv_clientid='56c46db2dd',
    hv_clientid_time=NOW(),
    hv_lastcheck=NOW() 
WHERE 
    hv_callid!=''
    AND is_done='N'
    AND (hv_lastcheck
        BETWEEN DATE_SUB(NOW(),INTERVAL 8 HOUR)
        AND DATE_SUB(NOW(),INTERVAL 120 SECOND)
    OR hv_lastcheck='0000-00-00 00:00:00')
    AND (
        hv_clientid IS NULL
        OR (
            hv_clientid_time IS NOT NULL
            && hv_clientid_time<DATE_SUB(NOW(),INTERVAL 2 MINUTE)
        )
    )
LIMIT 60

Query 2:

UPDATE actions SET
    hv_clientid=NULL,
    result='answer',
    is_done='Y',
    hv_callid=''
WHERE action_id='145291132'

We have multiple instances of a script that checks for statuses to update. Query 1 should grab rows, which it then selects based on the hv_clientid that is set. Once that select iterates over the data it runs Query 2.

Why would this be happening?

Was it helpful?

Solution

From MySQL Documentation - 14.2.7.9 How to Cope with Deadlocks (highlight added):

When modifying multiple tables within a transaction, or different sets of rows in the same table, do those operations in a consistent order each time. Then transactions form well-defined queues and do not deadlock. For example, organize database operations into functions within your application, or call stored routines, rather than coding multiple similar sequences of INSERT, UPDATE, and DELETE statements in different places.

If this is the cause, adding an ORDER BY clause to Query 1 should help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top