<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>N&#43;1 on Metudu</title>
    <link>https://metudu.github.io/tags/n&#43;1/</link>
    <description>Recent content in N&#43;1 on Metudu</description>
    <generator>Hugo -- 0.152.2</generator>
    <language>en</language>
    <lastBuildDate>Tue, 11 Nov 2025 13:59:04 +0300</lastBuildDate>
    <atom:link href="https://metudu.github.io/tags/n+1/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>What Is N&#43;1 Problem?</title>
      <link>https://metudu.github.io/posts/what-is-n&#43;1-problem/</link>
      <pubDate>Tue, 11 Nov 2025 13:59:04 +0300</pubDate>
      <guid>https://metudu.github.io/posts/what-is-n&#43;1-problem/</guid>
      <description>&lt;h2 id=&#34;understanding-n1&#34;&gt;Understanding N+1&lt;/h2&gt;
&lt;p&gt;The N+1 problem occurs when you make &lt;strong&gt;one&lt;/strong&gt; query to fetch &lt;em&gt;N&lt;/em&gt; objects, and then loop through those objects and make &lt;strong&gt;N additional queries&lt;/strong&gt;. When combined, this results in a total of &lt;strong&gt;N+1 queries&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&#34;illustrating-with-an-example&#34;&gt;Illustrating with an example&lt;/h2&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;books&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;db&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;getBooks&lt;/span&gt;() 
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here, we are simply retrieving all the books from the database. this is &lt;strong&gt;one&lt;/strong&gt; simple query that returns &lt;strong&gt;N&lt;/strong&gt; objects.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;index&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;book&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;:=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;range&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;books&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;authors&lt;/span&gt; = append(&lt;span style=&#34;color:#a6e22e&#34;&gt;authors&lt;/span&gt;, &lt;span style=&#34;color:#a6e22e&#34;&gt;db&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;getAuthor&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;book&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;author_id&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What we just did is iterate through the N books and fetch the author for each one. The &lt;code&gt;db.getAuthor()&lt;/code&gt; function run &lt;strong&gt;N times&lt;/strong&gt;, which means we executed &lt;strong&gt;N additional queries&lt;/strong&gt;.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
