Skip to content

Avoid N+1 queries in JiraConnect::client

Andy Schoenen requested to merge performance_improve_jira_connect_client into master

What does this MR do?

When syncing MRs with Jira, we expose a user notes count but this produces an N+1 query problem when the notes are not preloaded.

This MR adds an option to the PullRequest entity which can take a list of preloaded user notes counts. This way we can load all the counts in one query and pass it on to the entity.

Database query for notes count

SELECT
    COUNT(*) AS count_all,
    notes.noteable_id AS notes_noteable_id
FROM
    notes
WHERE
    notes.noteable_type = 'MergeRequest'
    AND notes.noteable_id IN (46783, 46784, 46785, 46786)
    AND notes.system = FALSE
GROUP BY
    notes.noteable_id;

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

(This is for 4 MRs. I just used random IDs)

 Aggregate  (cost=0.57..161.95 rows=34 width=12) (actual time=11.975..11.977 rows=0 loops=1)
   Group Key: notes.noteable_id
   Buffers: shared hit=15 read=6
   I/O Timings: read=11.800
   ->  Index Scan using index_notes_on_noteable_id_and_noteable_type on public.notes  (cost=0.57..161.44 rows=34 width=4) (actual time=11.972..11.973 rows=0 loops=1)
         Index Cond: ((notes.noteable_id = ANY ('{46783,46784,46785,46786}'::integer[])) AND ((notes.noteable_type)::text = 'MergeRequest'::text))
         Filter: (NOT notes.system)
         Rows Removed by Filter: 6
         Buffers: shared hit=15 read=6
         I/O Timings: read=11.800

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 Andy Schoenen

Merge request reports