Skip to content

Preload merge request diffs for Throughtput table

What does this MR do?

This MR preloads merge requests diffs on the DiffStatsSummary field of MergeRequest type (GraphQL), in order to reduce the number of queries that occur with loading the Throughput table on MR Analytics page.

This is towards #243709 (closed)

Example GraphQL query

{
  project(fullPath:"gitlab-org/gitlab-test") {
    mergeRequests {
      nodes {
        id
        diffStats {
          additions
          deletions
        }
      }      
    }
  }
}

Before this change the query above generates N+1 queries on the merge_request_diffs and the merge_request_diff_commits tables.

Database query

We expect maximum 100 merge request ids (max items per page in GraphQL)

SELECT "merge_request_diffs".*
FROM "merge_request_diffs"
INNER JOIN
  (SELECT DISTINCT FIRST_VALUE(id) OVER (PARTITION BY merge_request_id
                                         ORDER BY created_at DESC) AS id
   FROM "merge_request_diffs"
   WHERE "merge_request_diffs"."merge_request_id" IN (74672729,
                                                      74671284,
                                                      74670720,
                                                      74670050,
                                                      74667320,
                                                      74666241,
                                                      74665211,
                                                      74664673,
                                                      74664391,
                                                      74659727,
                                                      74659453,
                                                      74658415,
                                                      74657110,
                                                      74656781,
                                                      74656119,
                                                      74654621,
                                                      74653759,
                                                      74653405,
                                                      74653076,
                                                      74652570)) latest_diffs ON latest_diffs.id = merge_request_diffs.id

Plan: https://explain.depesz.com/s/X0zY

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
Edited by Adam Hegyi

Merge request reports