<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hassan Farooq</title>
    <description>The latest articles on DEV Community by Hassan Farooq (@hasan_dev).</description>
    <link>https://dev.to/hasan_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3075821%2F0a6ce578-0b1b-4ce5-ab50-236751c16efc.jpg</url>
      <title>DEV Community: Hassan Farooq</title>
      <link>https://dev.to/hasan_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hasan_dev"/>
    <language>en</language>
    <item>
      <title>Rails Interview #1: The N+1 Query Problem</title>
      <dc:creator>Hassan Farooq</dc:creator>
      <pubDate>Sun, 21 Jun 2026 19:18:46 +0000</pubDate>
      <link>https://dev.to/hasan_dev/rails-interview-1-the-n1-query-problem-5ei8</link>
      <guid>https://dev.to/hasan_dev/rails-interview-1-the-n1-query-problem-5ei8</guid>
      <description>&lt;p&gt;You ship a &lt;code&gt;/posts&lt;/code&gt; index page. It renders 50 posts, and for each one it shows the author's name with &lt;code&gt;post.author.name&lt;/code&gt;. QA says the page is slow, and the logs are full of repetitive SQL.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is this problem called, and why is it happening?&lt;/li&gt;
&lt;li&gt;How would you detect it, in dev and in a running app?&lt;/li&gt;
&lt;li&gt;How do you fix it, and what's the difference between the main fixing strategies?&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Answer: N+1 queries
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is and why it happens&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the N+1 query problem. One query loads the 50 posts, then Active Record fires one more query per post to load &lt;code&gt;post.author&lt;/code&gt;. That's 1 + N queries (1 for posts, 50 for authors) when it could be 2, or even 1. Active Record associations are lazy by default, so the author isn't loaded until you call &lt;code&gt;post.author&lt;/code&gt;. Do that inside a loop and you get a round trip per record.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to detect it&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dev logs: you'll see the same &lt;code&gt;SELECT ... FROM authors WHERE id = ?&lt;/code&gt; over and over with different IDs. That repetition is the tell.&lt;/li&gt;
&lt;li&gt;The Bullet gem: built for this. It warns you in dev when you should add eager loading, and also when you're eager-loading something you don't need.&lt;/li&gt;
&lt;li&gt;An APM in production (Sentry, New Relic, Scout): flags endpoints firing a lot of queries.&lt;/li&gt;
&lt;li&gt;Tests: assert on query count with something like &lt;code&gt;assert_queries&lt;/code&gt; or an RSpec matcher so CI catches a regression before it ships.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to fix it&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Eager-load the association:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# N+1&lt;/span&gt;
&lt;span class="vi"&gt;@posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
&lt;span class="vi"&gt;@posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;# 1 + 50 queries&lt;/span&gt;

&lt;span class="c1"&gt;# Fixed&lt;/span&gt;
&lt;span class="vi"&gt;@posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="vi"&gt;@posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;# 2 queries&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are four tools, and they don't do the same thing:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;th&gt;When to use it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;preload&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Loads the association in a separate query and matches it in memory&lt;/td&gt;
&lt;td&gt;You just need the data and aren't filtering or ordering by the association&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;eager_load&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One &lt;code&gt;LEFT OUTER JOIN&lt;/code&gt; that loads everything in a single query&lt;/td&gt;
&lt;td&gt;You need to filter or order by the association &lt;em&gt;and&lt;/em&gt; read its attributes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;includes&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Defaults to &lt;code&gt;preload&lt;/code&gt;, but switches to &lt;code&gt;eager_load&lt;/code&gt; if you reference the association in a &lt;code&gt;where&lt;/code&gt; or &lt;code&gt;order&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Your usual default. Let Rails decide&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;joins&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;INNER JOIN&lt;/code&gt; for filtering or sorting. Does not load the association into memory&lt;/td&gt;
&lt;td&gt;You need to filter by the association but don't read its attributes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The trap: &lt;code&gt;joins&lt;/code&gt; alone does not preload. If you &lt;code&gt;joins(:author)&lt;/code&gt; and then call &lt;code&gt;p.author.name&lt;/code&gt; in the loop, you're right back to N+1. Reach for &lt;code&gt;includes&lt;/code&gt; or &lt;code&gt;eager_load&lt;/code&gt; when you actually read the association's attributes.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>database</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
