Skip to content

Commit 7af8fa7

Browse files
yoffadityasharad
andauthored
Apply suggestions from code review
Co-authored-by: Aditya Sharad <[email protected]>
1 parent 5feb401 commit 7af8fa7

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

ruby/ql/src/queries/performance/CouldBeHoisted.qhelp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55

66
<overview>
77
<p>
8-
When a Rails ActiveRecord query is executed in a loop, it is potentially an n+1 problem.
9-
This query identifies situations where an ActiveRecord query execution could be pulled out of a loop.
8+
When a database query operation, for example a call to a query method in the Rails `ActiveRecord::Relation` class, is executed in a loop, this can lead to a performance issue known as an "n+1 query problem".
9+
The database query will be executed in each iteration of the loop.
10+
Performance can usually be improved by performing a single database query outside of a loop, which retrieves all the required objects in a single operation.
1011
</p>
1112
</overview>
1213
<recommendation>
13-
<p>If possible, pull the query out of the loop, thus replacing the many calls with a single one.
14+
<p>If possible, pull the database query out of the loop and rewrite it to retrieve all the required objects. This replaces multiple database operations with a single one.
1415
</p>
1516
</recommendation>
1617
<example>
1718
<p>The following (suboptimal) example code queries the User object in each iteration of the loop:</p>
1819
<sample src="examples/straight_loop.rb" />
19-
<p>To improve the performance, we instead query the User object once outside the loop, gathereing all necessary information:</p>
20+
<p>To improve the performance, we instead query the <code>User</code> object once outside the loop, gathering all necessary information in a single query:</p>
2021
<sample src="examples/preload.rb" />
2122
</example>
2223
</qhelp>

ruby/ql/src/queries/performance/CouldBeHoisted.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ where
8787
// Only report calls that are likely to be expensive
8888
call instanceof ActiveRecordModelFinderCall and
8989
not call.getMethodName() in ["new", "create"]
90-
select call, "This call happens inside $@, and could be hoisted.", loop, "this loop"
90+
select call, "This call to a database query operation happens inside $@, and could be hoisted to a single call outside the loop.", loop, "this loop"

0 commit comments

Comments
 (0)