Skip to content

Commit f6f7747

Browse files
committed
optimise query
1 parent 43ad8b3 commit f6f7747

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

app/api/views/users.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ class TelegramUserViewSet(viewsets.ReadOnlyModelViewSet):
5454
ordering = ['-last_seen']
5555

5656
def get_queryset(self):
57+
# No .distinct() here: the channel/tag filters join rows that are
58+
# unique per user (memberships are unique_together (user, channel);
59+
# tags are an M2M through table), so they cannot duplicate users.
60+
# DISTINCT forced DRF's pagination COUNT into
61+
# COUNT(*) FROM (SELECT DISTINCT <all columns> ...), detoasting every
62+
# profile_photo_base64 blob in the table — ~30 s per page load.
5763
return TelegramUser.objects.all().annotate(
5864
group_count_annotation=Count('memberships', distinct=True)
59-
).distinct()
65+
)
6066

6167
def get_serializer_class(self):
6268
if self.action == 'retrieve':

0 commit comments

Comments
 (0)