Optimize campaign count retrieval in ChannelListView

This commit is contained in:
Joakim Hellsén 2026-01-07 21:32:28 +01:00
commit ed4f42052b
No known key found for this signature in database
2 changed files with 180 additions and 11 deletions

View file

@ -831,11 +831,13 @@ class ChannelListView(ListView):
if search_query:
queryset = queryset.filter(Q(name__icontains=search_query) | Q(display_name__icontains=search_query))
campaign_count_subquery: QuerySet[DropCampaign, dict[str, Any]] = (
DropCampaign.objects
.filter(allow_channels=OuterRef("pk"))
.values("allow_channels")
.annotate(count=Count("pk"))
# Count directly from the through table for maximum efficiency
# This avoids unnecessary JOINs and GROUP BY operations
campaign_count_subquery = (
DropCampaign.allow_channels.through.objects
.filter(channel_id=OuterRef("pk"))
.values("channel_id")
.annotate(count=Count("id"))
.values("count")
)