Replace expensive IN check with JOIN for merge requests in group query
What does this MR do and why?
This change introduces an alternative way to find projects within a group and its subgroups when searching for merge requests.
The original method resulted in a database query that involved an namespace_id IN check on either the cached namespace_descendants table or a namespace traversal check to find all projects in a group hierarchy.
The new approach uses a join on namespaces the premise that each project belongs to a namespace and has a project_namespace_id. From the resulting join, we can check if the group ID is present in the traversal_ids entry for the project.
This improvement is implemented as a feature flag, meaning it can be turned on or off without deploying new code. Currently, the feature is disabled by default and only applies to merge request searches. The code includes proper testing to ensure both the old and new methods return the same results, guaranteeing that users won't see any difference in functionality while the performance optimization is being tested and rolled out gradually.
References
Spike: identify opportunities to improve top-le... (#585222)
Query plans
old query: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/49426/commands/147417
new query: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/49426/commands/147416
Screenshots or screen recordings
| Before | After |
|---|---|
How to set up and validate locally
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.