Skip to content

Fix more N+1 on notification settings page

Adam Hegyi requested to merge 21043-fix-more-n-plus-one-queries into master

What does this MR do?

This MR fixes N+1 queries in the Profiles::NotificationsController#show action. The previous MR addressed N+1 queries related to @group_notifications. This one fixes N+1 queries for @project_notifications.

Page: https://gitlab.com/-/profile/notifications

There are two fixes:

  • Preload namespace (and owner) or group with routes for the source (project).
  • Preload MAX("project_authorizations"."access_level") calculation for an array of projects by storing them in RequestStore.
    • The query is called deep within the policy, so it's difficult to preload it.

Screenshots (strongly suggested)

Before:

image

After:

image

DB query:

SELECT MAX("project_authorizations"."access_level") AS maximum_access_level,   "project_authorizations"."project_id" AS project_authorizations_project_id
FROM "project_authorizations"
WHERE "project_authorizations"."user_id" = 4156052
  AND "project_authorizations"."project_id" IN (13083, 13764, 14022, 14288, 14289)
GROUP BY "project_authorizations"."project_id"

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related to #21043 (closed)

Edited by Adam Hegyi

Merge request reports