Skip to content

Optimise annotate_feature_queryset_with_code_references_summary for feature list endpoint #6832

@gagantrivedi

Description

@gagantrivedi

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:

  1. jsonb_path_exists correlated subquery — for every feature row, it scans FeatureFlagCodeReferencesScan.code_references JSON to find the last scan containing that feature name.
  2. jsonb_path_query_array + jsonb_array_length correlated subquery — for every feature row, it extracts and counts matching references from the JSON column.
  3. Nested subquerylast_feature_found_at is itself a subquery nested inside the counts_by_repository subquery, 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions