Fix N+1 queries in work items REST API

What does this MR do and why?

Fix N+1 queries in work items REST API

  • Pass resource_parent option to labels entity to check scoped_labels license once per request instead of per work item
  • Add context-aware preloads for project vs group work items
  • Preload namespace.route for URL/reference generation
  • Preload milestone.project/group for milestone web_url
  • Add shared N+1 query prevention test

References

Screenshots or screen recordings

Before After
Screenshot_2026-02-25_at_11.44.45 Screenshot_2026-02-25_at_11.59.36

How to set up and validate locally

  1. Enable work_item_rest_api feature flag
  2. Compare the amount of queries for the request between master and this branch: http://gdk.test:3000/api/v4/namespaces/gitlab/-/work_items?features=milestone,labels&fields=promoted_to_epic_url,web_url,web_path,reference,create_note_email,work_item_type&per_page=100
  3. You can compare it by copying the correlation-id from the request header, and add it to the performance bar

Screenshot_2026-02-25_at_12.02.35

Screenshot_2026-02-25_at_12.02.56

Screenshot_2026-02-25_at_12.03.01

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Nicolas Dular

Merge request reports

Loading