I have question about mysql/innodb locking when selecting "FOR UPDATE" in a 2 table join query. Example:
2 tables - items
, queue
. queue.id
is 1:1 connection to items.id (FOREIGN KEY). queue.status
is enum with index. queue has very little number of rows, while items table is relatively large.
SELECT *
FROM `items`
INNER JOIN queue
ON items.id = queue.id
WHERE queue.status = 'new'
FOR UPDATE;
- Will selected rows in table
items
be exclusively locked? - What other locks are obtained? (except obviously all rows in
queue
table that have stats='new'). - Are there any shared locks that could cause deadlock. I've read somewhere that insert puts next key shared lock on the autoincrement index and then when using
SELECT ... FOR UPDATE
(in the same transaction) the lock is upgraded it to exclusive, it's easy a deadlock to happen - 2 threads can get the shared lock and then they both will wait each other to release the lock in order to get the exclusive lock. is it possible in this case (also heard that foreign keys make shared locks).