4

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;
  1. Will selected rows in table items be exclusively locked?
  2. What other locks are obtained? (except obviously all rows in queue table that have stats='new').
  3. 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).
NickSoft
  • 3,215
  • 5
  • 27
  • 48

1 Answers1

0

Yes every selected row(*) will be locked. You really do not need to worry about a deadlock. It takes quite a lot to create one and when it does happend, it's mainly the clients fault.