retention: Avoid "SELECT realm" being run by Django ORM for each stream.

The  way the code changed in this commit was written caused Django to
fetch stream.realm from the database for every stream, leading to
redundant, identical queries. Each stream's realm is already known, so
we use that information.
This commit is contained in:
Mateusz Mandera 2019-07-03 19:54:56 +02:00 committed by Tim Abbott
parent 8af03b92a0
commit b55f24e07c
1 changed files with 7 additions and 6 deletions

View File

@ -3,7 +3,7 @@ from datetime import timedelta
from django.conf import settings
from django.db import connection, transaction
from django.db.models import Model, Q
from django.db.models import Model
from django.utils.timezone import now as timezone_now
from zerver.lib.logging_util import log_to_file
@ -260,17 +260,18 @@ def archive_stream_messages(realm: Realm, chunk_size: int=MESSAGE_BATCH_SIZE) ->
logger.info("Archiving stream messages for realm " + realm.string_id)
# We don't archive, if the stream has message_retention_days set to -1,
# or if neither the stream nor the realm have a retention policy.
streams = Stream.objects.exclude(message_retention_days=-1).filter(
Q(message_retention_days__isnull=False) | Q(realm__message_retention_days__isnull=False),
realm_id=realm.id
)
streams = Stream.objects.filter(realm_id=realm.id).exclude(message_retention_days=-1)
if not realm.message_retention_days:
streams = streams.exclude(message_retention_days__isnull=True)
retention_policy_dict = {} # type: Dict[int, int]
for stream in streams:
# if stream.message_retention_days is null, use the realm's policy
if stream.message_retention_days:
retention_policy_dict[stream.id] = stream.message_retention_days
else:
retention_policy_dict[stream.id] = stream.realm.message_retention_days
assert realm.message_retention_days is not None
retention_policy_dict[stream.id] = realm.message_retention_days
recipients = get_stream_recipients([stream.id for stream in streams])
message_count = 0