Suggest using preload to avoid N+1 cached query
What does this MR do and why?
In the document https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/development/merge_request_performance_guidelines.md#cached-queries, we have suggested to
pipeline.builds.each do |build| build.project = pipeline.project build.to_json(only: [:name], include: [project: { only: [:name]}]) end
I think this is less intuitive, because it requires us to first assume build.project
MUST be pipeline.project
. I think the easier approach is to preload the required association:
# preload the required associations
ActiveRecord::Associations::Preloader.new.preload(pipeline, [builds: :project])
pipeline.builds.each do |build|
build.to_json(only: [:name], include: [project: { only: [:name]}])
end
BTW: if we search in Gitlab source code, we could see there are many places already used ActiveRecord::Associations::Preloader.new.preload
.
We could verity preload
behaves as expected, refer to this comment.
Screenshots or screen recordings
These are strongly recommended to assist reviewers and reduce the time to merge your change.
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.