0
0
Ruby on Railsframework~10 mins

Eager loading (N+1 prevention) in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Eager loading (N+1 prevention)
Query Parent Records
Check for Associated Records
Without Eager Loading
For each parent, query associated
Many queries (N+1 problem)
Slow performance
Shows how fetching associated records separately causes many queries (N+1 problem), while eager loading fetches all at once for speed.
Execution Sample
Ruby on Rails
posts = Post.all
posts.each do |post|
  puts post.comments.count
end
This code fetches all posts, then for each post fetches its comments count, causing many queries (N+1 problem).
Execution Table
StepActionQuery ExecutedNumber of Queries So FarResult
1Fetch all postsSELECT * FROM posts;1Posts loaded
2For post 1, fetch comments countSELECT COUNT(*) FROM comments WHERE post_id = 1;2Comments count for post 1
3For post 2, fetch comments countSELECT COUNT(*) FROM comments WHERE post_id = 2;3Comments count for post 2
4For post 3, fetch comments countSELECT COUNT(*) FROM comments WHERE post_id = 3;4Comments count for post 3
5End loopNo query4All posts processed
💡 Loop ends after all posts processed; total queries = 1 (posts) + N (comments per post)
Variable Tracker
VariableStartAfter 1After 2After 3Final
postsnil[post1, post2, post3][post1, post2, post3][post1, post2, post3][post1, post2, post3]
postnilpost1post2post3nil
comments_countnilcount for post1count for post2count for post3nil
Key Moments - 2 Insights
Why does the code run one query per post to get comments count?
Because inside the loop, calling post.comments.count triggers a separate query each time, as shown in execution_table rows 2-4.
How does eager loading fix the N+1 problem?
Eager loading fetches all comments for all posts in one query before the loop, avoiding multiple queries during iteration.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many queries run after fetching 3 posts and their comments counts without eager loading?
A1
B3
C4
D6
💡 Hint
Check the 'Number of Queries So Far' column in the execution_table after step 4.
If we add eager loading with Post.includes(:comments), how many queries will run for 3 posts and their comments?
A1
B2
C3
D4
💡 Hint
Eager loading runs one query for posts and one for comments, see key_moments explanation.
In variable_tracker, what is the value of 'post' after step 3?
Apost3
Bpost2
Cpost1
Dnil
💡 Hint
Look at the 'post' row and the 'After 3' column in variable_tracker.
Concept Snapshot
Eager loading prevents N+1 queries by fetching associated records in advance.
Without it, each parent record triggers a query for its children.
Use Post.includes(:comments) to load posts and comments in two queries.
This improves performance by reducing database hits.
Always eager load when accessing associations in loops.
Full Transcript
This visual execution shows how fetching posts and their comments without eager loading causes multiple queries, one per post, known as the N+1 problem. The code first runs one query to get all posts, then inside a loop, each post triggers a separate query to count its comments. This leads to slow performance. Eager loading solves this by running one query for posts and one for all comments, reducing queries drastically. The variable tracker shows how variables change during execution. The key moments clarify why queries multiply and how eager loading fixes it. The quizzes test understanding of query counts and variable states.