Optimize how we fetch the latest (current) revision

The way were were doing it was implicitly sorting the query items and it
was causing millions of items to be sorted (even though the result should
only have one) making it slow.

By switching away from `get()` and `first()` we are telling django to
not try to sort.
master
Tom Hacohen 2 years ago
parent 5f455e55b5
commit 2f1f95fea9

@ -96,7 +96,7 @@ class CollectionItem(models.Model):
@cached_property
def content(self) -> "CollectionItemRevision":
return self.revisions.get(current=True)
return self.revisions.filter(current=True)[0]
@property
def etag(self) -> str:

@ -396,7 +396,7 @@ def item_create(item_model: CollectionItemIn, collection: models.Collection, val
if not created:
# We don't have to use select_for_update here because the unique constraint on current guards against
# the race condition. But it's a good idea because it'll lock and wait rather than fail.
current_revision = instance.revisions.filter(current=True).select_for_update().first()
current_revision = instance.revisions.filter(current=True).select_for_update()[0]
assert current_revision is not None
current_revision.current = None
current_revision.save()

Loading…
Cancel
Save