Skip to content

Backend: ReleasesFinder improvement to reduce queries

The following discussion from Add GraphQL field `versions` to CiCatalogResource (!120811 - merged) should be addressed:

This line seems to introduce a new list of projects.

I think it should be possible to reuse the existing list @parent in this finder so that all the preload can be done for @parent instead, and this @parent then can be reused by the releases (releases repeats the preload again for their project association).

To reuse the existing projects for releases, it can be done in either the finder or the controller as follow:

releases.each do |release|
  # This is the way to assign the existing projects from `@parent` to release
  # so that calling `release.project` won't trigger any database queries
  # if the existing project is preloaded using `Preloaders::ProjectPolicyPreloader.new(@parent, current_user).execute`
  # calling e.g. `release.project.project_feature` won't trigger any database queries either
  release.association(:project).target = existing_project_from_parent_variable
end

Since the N+1 tests are in place, I believe no new N+1 queries can be introduced.

Implementation

The second suggestion above (to re-associate the existing list of projects) is no longer applicable because BatchLoader was removed from the ReleasesResolver. Applying that now would effectively remove keyset pagination. As such, only the first comment above was addressed and a new projects list is no longer introduced with this change. See discussion here: !123991 (comment 1434800305)

Description MR
Update ReleasesFinder to reduce queries !123991 (closed)
[2023-07-14] UPDATE

Upon further discussion in !123991 (closed), it was discovered that the change to reduce the project queries by 1 could end up adding more queries down the line in the workflow. So the first suggestion described in this issue is no longer cost effective in terms of performance. See !123991 (comment 1469229095). Closing this issue. 🤗

Edited by Leaminn Ma