Python Forum

Full Version: Django. Lock records without actual selecting
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I want to lock thousands of records(using select_for_update), but I don't really need that records will be extracted from database.
How can I lock records and not to waste time for extracting, creating objects and etc?

My current code for locking:
qs = cls.objects.select_for_update().filter(namespace__in[namespace1, namespace2])
records = list(qs.all())
I don't know a lot about Django, but can you just select the row's id? Getting a few thousand ints from the db shouldn't be a big deal. But if you're not updating the row, why do you need to lock it? Without knowing more about what you're doing, it sounds like you might be using the wrong tool for what you're trying to accomplish.

Also, not all database engines support row-level locks.

But regardless, this article has more info on row-level locking with Django: https://medium.com/@hakibenita/how-to-ma...240fed4ee2
(Dec-19-2018, 08:31 PM)nilamo Wrote: [ -> ]I don't know a lot about Django, but can you just select the row's id? Getting a few thousand ints from the db shouldn't be a big deal. But if you're not updating the row, why do you need to lock it? Without knowing more about what you're doing, it sounds like you might be using the wrong tool for what you're trying to accomplish.

Also, not all database engines support row-level locks.

But regardless, this article has more info on row-level locking with Django: https://medium.com/@hakibenita/how-to-ma...240fed4ee2

Thank you for your reply!

All rows are going to be updated.
All updates in one transaction.

As DB engine I'm using InnoDB (MySQL).
But in plans to support PostgreSQL too.


If there is no a better solution,
I'll change it on selecting only primary key
and extracting result as flat list instead of objects (save time for an object initialization)

Also I want to try to use iterator for getting only one record from buffered result and not to create even a flat list.


After locking there are 5 operations on update with different conditions.
# 2 updates for allocating some logical place in namespace1
pos = cls.__allocate(namespace1, pos, pos, size)

# 1 update
self.select_related_tags().update(
	low  = F( "low"  ) + delta,
	high = F( "high" ) + delta,
	**update_fields_ex
)

# 2 updates for deleting free space
cls.__free(namespace2, low, high, -size)