-
Notifications
You must be signed in to change notification settings - Fork 474
Open
Description
Summary
annotate_feature_queryset_with_code_references_summary in api/projects/code_references/services.py adds expensive per-row annotations to the feature list queryset, slowing down the GET /api/v1/projects/{project_id}/features/ endpoint.
Problem
The function annotates each feature with code_references_counts using:
jsonb_path_existscorrelated subquery — for every feature row, it scansFeatureFlagCodeReferencesScan.code_referencesJSON to find the last scan containing that feature name.jsonb_path_query_array+jsonb_array_lengthcorrelated subquery — for every feature row, it extracts and counts matching references from the JSON column.- Nested subquery —
last_feature_found_atis itself a subquery nested inside thecounts_by_repositorysubquery, compounding the cost.
These JSON path operations run per-feature, per-repository and do not benefit from standard indexes. For projects with many features or many scan records, this results in significant query time even when code references are unused.
Current mitigation
An early-exit check (has_scans) skips annotation when no scans exist for the project, but this doesn't help projects that do have scans.
Location
- Function:
annotate_feature_queryset_with_code_references_summaryinapi/projects/code_references/services.py:31 - Called from:
api/features/views.py:220
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels