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

@@ -845,7 +845,7 @@ def get_occupied_streams(realm: Realm) -> QuerySet[Stream]:
)
occupied_streams = (
Stream.objects.filter(realm=realm, deactivated=False)
.annotate(occupied=exists_expression)
.alias(occupied=exists_expression)
.filter(occupied=True)
)
return occupied_streams
@@ -1002,7 +1002,7 @@ def get_subscribed_private_streams_for_user(user_profile: UserProfile) -> QueryS
)
subscribed_private_streams = (
Stream.objects.filter(realm=user_profile.realm, invite_only=True, deactivated=False)
.annotate(subscribed=exists_expression)
.alias(subscribed=exists_expression)
.filter(subscribed=True)
)
return subscribed_private_streams