'MySQL for update prevent lock joined tables

I need to process a queue and MySQL for update is the goal for that.

Everything is working fine, except the fact for update is locking joined tables too.

I have 4 functions that are called simultaneously and 2 of them joins the same table.

The problem is that it seems it's locking all joined table and not specific joined rows.

My code is something like that (just example):

Query #1:

SELECT t1.id, t1.name, t2.balance FROM t1
LEFT JOIN t2 ON t2.user_id = t1.id

WHERE t2.balance < 0

LIMIT 10

FOR UPDATE
SKIP LOCKED

Query #2:

SELECT t3.id, t3.action, t2.balance FROM t3
LEFT JOIN t2 ON t2.user_id = t3.user_id

WHERE t2.balance < 0

LIMIT 10

FOR UPDATE
SKIP LOCKED

I didn't find anything related to this.

Is there a way to avoid that behavior?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source