Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform SELECT FOR UPDATE with SKIP LOCKED on MySQL with Django

I have a Django project which uses a MySQL v5.5 backend with InnoDB storage.
To avoid race condition updates in DB, I'm using select_for_update to lock the rows. Now if this lock stays for a long time, any queries on the locked rows will timeout.
I want to avoid this with one of the following options:

  1. Skip the rows which are locked, similar to SKIP LOCKED option.
  2. Return immediately if some rows you're querying are locked, similar to NOWAIT option.
  3. Reduce the lock wait timeout period for specific queries.

How do I perform any of these with Django ORM?

like image 697
akhil_ Avatar asked Mar 09 '18 19:03

akhil_


1 Answers

from Django docs:

You can also ignore locked rows by using select_for_update(skip_locked=True)

like image 82
Intey Avatar answered Nov 02 '22 05:11

Intey