From 35559ae3249a900cbed17c1dbe9668697fd15400 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Tue, 1 Aug 2023 15:40:55 +0530 Subject: [PATCH] export: Pass "user_profile" as arg to select_related call for Subscription. This commit adds code to pass all the required arguments to select_related call for Subscription objects such that only the required related fields are fetched from the database. Previously, we did not pass any arguments to select_related, so all the directly and indirectly related fields were fetched when many of them were actually not being used and made the query unnecessarily complex. --- zerver/lib/export.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zerver/lib/export.py b/zerver/lib/export.py index f688b28d2c..8526801ce2 100644 --- a/zerver/lib/export.py +++ b/zerver/lib/export.py @@ -1120,10 +1120,10 @@ def custom_fetch_huddle_objects(response: TableData, context: Context) -> None: # Mark all Huddles whose recipient ID contains a cross-realm user. unsafe_huddle_recipient_ids = set() - for sub in Subscription.objects.select_related().filter( + for sub in Subscription.objects.select_related("user_profile").filter( recipient__in=realm_huddle_recipient_ids ): - if sub.user_profile.realm != realm: + if sub.user_profile.realm_id != realm.id: # In almost every case the other realm will be zulip.com unsafe_huddle_recipient_ids.add(sub.recipient_id)