analytics: Pass subgroup=None to improve indexing.

Because the main indexes on end_time either don't include realm_id or
do include subgroup, passing an explicit subgroup=None for
single-realm queries to read CountStats that don't use the subgroups
feature greatly improves the query plans.
This commit is contained in:
Tim Abbott
2024-10-02 10:40:59 -07:00
committed by Alex Vandiver
parent 9c8cc8c333
commit 1ff14fd0f1
4 changed files with 17 additions and 2 deletions

View File

@@ -67,7 +67,13 @@ def get_active_realm_ids() -> QuerySet[RealmCount, int]:
"""
date = timezone_now() - timedelta(days=2)
return (
RealmCount.objects.filter(end_time__gte=date, property="1day_actives::day", value__gt=0)
RealmCount.objects.filter(
end_time__gte=date,
property="1day_actives::day",
# Filtering on subgroup is important to ensure we use the good indexes.
subgroup=None,
value__gt=0,
)
.distinct("realm_id")
.values_list("realm_id", flat=True)
)