streams: Tweak query in update_stream_active_status_for_realm.

The NOT EXISTS structure might be better optimized by the Postgres query
planner and might lead to slightly better performance than the id NOT IN
(<subquery>) structure.
This commit is contained in:
Mateusz Mandera
2024-12-13 20:33:03 +01:00
committed by Tim Abbott
parent f7e02c51d2
commit e41ee8abf5

View File

@@ -1258,15 +1258,14 @@ def get_subscribed_private_streams_for_user(user_profile: UserProfile) -> QueryS
@transaction.atomic(durable=True)
def update_stream_active_status_for_realm(realm: Realm, date_days_ago: datetime) -> int:
active_stream_ids = (
Message.objects.filter(
date_sent__gte=date_days_ago, recipient__type=Recipient.STREAM, realm=realm
)
.values_list("recipient__type_id", flat=True)
.distinct()
recent_messages_subquery = Message.objects.filter(
date_sent__gte=date_days_ago,
realm=realm,
recipient__type=Recipient.STREAM,
recipient__type_id=OuterRef("id"),
)
streams_to_mark_inactive = Stream.objects.filter(is_recently_active=True, realm=realm).exclude(
id__in=active_stream_ids
streams_to_mark_inactive = Stream.objects.filter(
~Exists(recent_messages_subquery), is_recently_active=True, realm=realm
)
# Send events to notify the users about the change in the stream's active status.