Reduce N+1 approval rules related queries on GraphQL request
What does this MR do and why?
When querying merge requests via GraphQL and querying the mergeable
or detailedMergeStatus
fields, it can result to N+1 SQL queries when checking for the approval state of the MR.
This removes the N+1 queries problem by preloading associations that are being used in MergeRequests::Mergeability::CheckApprovedService
.
Data
Tested this locally with 5 MRs with the same approval rules by requesting the mergeable
field:
Overriding approval rules is disabled:
- Before: 80 SQL queries (0.7s total duration)
- After: 32 SQL queries (0.4s total duration)
Approval rules are overridden:
- Before: 84 SQL queries (0.5s duration)
- After: 30 SQL queries (0.4s duration)
Query plans
-
approvals
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80619 -
scan_result_policy_violations
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80620 -
applicable_post_merge_approval_rules
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80621 -
approval_merge_request_rules
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80622 -
approval_merge_request_rules_approved_approvers
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80623 -
approval_merge_request_rules_groups
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80624 -
approval_merge_request_rules_users
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80625 -
approval_merge_request_rule_sources
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80626 -
approval_project_rules
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80627 -
approval_project_rules_groups
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80628 -
approval_project_rules_users
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80629 -
approval_project_rules_protected_branches
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80630 -
protected_branches
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25984/commands/81741 -
latest_merge_request_diff
: https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/25456/commands/80632
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Related to #437975 (closed)