Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query optimization for the Briefcase viewset #2142

Merged
merged 6 commits into from
Oct 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions onadata/apps/api/viewsets/briefcase_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ def filter_queryset(self, queryset):
xform_kwargs['user__username__iexact'] = username
xform = get_form(xform_kwargs)
self.check_object_permissions(self.request, xform)
instances = Instance.objects.filter(xform=xform,
deleted_at__isnull=True)
instances = Instance.objects.filter(
xform=xform, deleted_at__isnull=True).values(
'pk', 'uuid')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using the uuid anywhere? We could just only return the pk.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, forgot about that.

if xform.encrypted:
instances = instances.filter(media_all_received=True)
instances = instances.order_by('pk')
Expand All @@ -143,10 +144,16 @@ def filter_queryset(self, queryset):
if num_entries:
instances = instances[:num_entries]

if instances.count():
last_instance = instances[instances.count() - 1]
self.resumptionCursor = last_instance.pk
elif instances.count() == 0 and cursor:
# Using len() instead of .count() to prevent an extra
# database call; len() will load the instances in memory allowing
# us to pre-load the queryset before generating the response
# and removes the need to perform a count on the database.
instance_count = len(instances)

if instance_count > 0:
last_instance = instances[instance_count - 1]
self.resumptionCursor = last_instance.get('pk')
elif instance_count == 0 and cursor:
self.resumptionCursor = cursor
else:
self.resumptionCursor = 0
Expand Down