perf(api): use literal scan_ids in finding-groups /latest aggregation (#11380)

This commit is contained in:
Adrián Peña authored and GitHub committed 2026-05-28 13:46:15 +02:00
1 parent a2824f7166
commit 81226cd837
1 file changed
+5 -2
+5 -2
View File
@@ -7484,14 +7484,17 @@ class FindingGroupViewSet(BaseRLSViewSet):
def _get_latest_findings_per_provider(self, filtered_queryset):
"""Keep only findings from each provider's most recent completed scan."""
latest_scan_ids = (
# Materialize to a literal IN list. Left as a subquery, Postgres can't
# estimate the match count and picks a serial nested loop on
# resource_finding_mappings when one scan dominates findings
latest_scan_ids = list(
Scan.objects.filter(
tenant_id=self.request.tenant_id,
state=StateChoices.COMPLETED,
)
.order_by("provider_id", "-completed_at", "-inserted_at")
.distinct("provider_id")
.values("id")
.values_list("id", flat=True)
)
return filtered_queryset.filter(scan_id__in=latest_scan_ids)