perf: run select count(*) only once for entitlements.count()

Not much of an optimization, but might shave a couple seconds if the
number of entitlements on production are large.

Django will use the _result_cache if the QuerySet has already been
retrieved:

107865780a/django/db/models/query.py (L597-L598)
This commit is contained in:
Phillip Shiu
2023-06-22 22:42:49 -04:00
parent d4ae7bfa06
commit eee3db8820

View File

@@ -85,7 +85,8 @@ class Command(BaseCommand):
enrollment_course_run__isnull=True, course_uuid__in=exceptional_courses)
entitlements = normal_entitlements | exceptional_entitlements
logger.info('Total entitlements that have reached expiration period are %d ', entitlements.count())
entitlements_count = entitlements.count()
logger.info('Total entitlements that have reached expiration period are %d ', entitlements_count)
entitlements_to_expire = max(1, options.get('count'))
batch_size = max(1, options.get('batch_size'))
@@ -102,7 +103,7 @@ class Command(BaseCommand):
for batch_num in range(num_batches):
start = batch_num * batch_size
end = min(start + batch_size, entitlements_to_expire, entitlements.count())
end = min(start + batch_size, entitlements_to_expire, entitlements_count)
expire_and_create_entitlements.delay(entitlements[start:end], support_user)
logger.info('Done. Successfully enqueued %d tasks.', num_batches)