Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b257c18f
Commit
b257c18f
authored
Jan 26, 2022
by
Qingyu Zhao
Committed by
Suzanne Selhorn
Jan 26, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Suggest using preload to avoid N+1 cached query
parent
05a72a43
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
4 deletions
+4
-4
doc/development/merge_request_performance_guidelines.md
doc/development/merge_request_performance_guidelines.md
+4
-4
No files found.
doc/development/merge_request_performance_guidelines.md
View file @
b257c18f
...
...
@@ -255,15 +255,15 @@ It re-instantiates project object for each build, instead of using the same in-m
In this particular case the workaround is fairly easy:
```
ruby
ActiveRecord
::
Associations
::
Preloader
.
new
.
preload
(
pipeline
,
[
builds: :project
])
pipeline
.
builds
.
each
do
|
build
|
build
.
project
=
pipeline
.
project
build
.
to_json
(
only:
[
:name
],
include:
[
project:
{
only:
[
:name
]}])
end
```
We can assign
`pipeline.project`
to each
`build.project`
, since we know it should point to the same project.
This allows us that each build point to the same in-memory project,
avoiding the cached SQL query and re-instantiation of the project object for each build.
`ActiveRecord::Associations::Preloader`
uses the same in-memory object for the same project.
This avoids the cached SQL query and also avoids re-instantiation of the project object for each build.
## Executing Queries in Loops
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment