1
0
mirror of https://github.com/etesync/server synced 2024-11-24 09:48:09 +00:00

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.
This commit is contained in:
Tom Hacohen 2022-06-19 22:25:49 +03:00
parent 5f455e55b5
commit 2f1f95fea9
2 changed files with 2 additions and 2 deletions

View File

@ -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:

View File

@ -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()