django: Switch to .alias() instead .annotate() where possible.

When using the sub-expression purely for filtering, and not for
accessing the value in the resultset, .alias() is potentially faster
since it does not pull the value in as well.
This commit is contained in:
Alex Vandiver
2024-07-11 15:04:53 +00:00
committed by Tim Abbott
parent e1a9473bd6
commit d5a4941691
7 changed files with 11 additions and 11 deletions

View File

@@ -203,7 +203,7 @@ def add_missing_messages(user_profile: UserProfile) -> None:
recipient_ids.append(sub["recipient_id"])
new_stream_msgs = (
Message.objects.annotate(
Message.objects.alias(
has_user_message=Exists(
UserMessage.objects.filter(
user_profile_id=user_profile,
@@ -213,7 +213,7 @@ def add_missing_messages(user_profile: UserProfile) -> None:
)
.filter(
# Uses index: zerver_message_realm_recipient_id
has_user_message=0,
has_user_message=False,
realm_id=user_profile.realm_id,
recipient_id__in=recipient_ids,
id__gt=user_profile.last_active_message_id,