<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Biswas Pandit on Medium]]></title>
        <description><![CDATA[Stories by Biswas Pandit on Medium]]></description>
        <link>https://medium.com/@biswaspandit1?source=rss-ae6cd6e0b939------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*F9tLAIIqe7b-rQL8</url>
            <title>Stories by Biswas Pandit on Medium</title>
            <link>https://medium.com/@biswaspandit1?source=rss-ae6cd6e0b939------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 23:08:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@biswaspandit1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Silent Database Killer: The N+1 Query Problem in EF Core]]></title>
            <link>https://medium.com/@biswaspandit1/the-silent-database-killer-the-n-1-query-problem-in-ef-core-ba44fbf156e4?source=rss-ae6cd6e0b939------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba44fbf156e4</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[performance-optimization]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <dc:creator><![CDATA[Biswas Pandit]]></dc:creator>
            <pubDate>Sat, 20 Jun 2026 20:49:54 GMT</pubDate>
            <atom:updated>2026-06-20T20:49:54.787Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nEab-KrdYzUZI46k22886A.png" /></figure><p>Your database might be slowly eaten alive by a hidden performance killer, without you even realising it. It’s called the <strong>N+1 query problem, </strong>and it’s the most common reason fast .NET applications suddenly crawl to a halt in production.</p><p><strong>What actually is the N+1 Query Problem?</strong></p><p>N+1 query problem happens when your application makes one initial query to fetch a list of records, and then executes an additional query for <strong>each </strong>item in that list to fetch itsrelated data. Think of it like going grocery shopping where you bring 50 different items to the counter. Instead of the cashier giving you a collective total of all 50 items, the cashier makes you pay for each item <strong>one at a time</strong> scanning and charging you separately for every single item. In software, this is exactly what happens: those individual payment are database roundtrips, which are incredibly expensive.</p><blockquote>The N+1 query problem is a performance bottleneck where an application makes N+1 database calls to retrieve data that could have been fetched in a single, unified query.</blockquote><p><strong>How the N+1 Problem Happens in .NET?</strong></p><p>Let’s look at a common Entity Framework Core scenario. Imagine a simple one-to-many relationship where a <strong>Blog</strong> has many <strong>Posts</strong>.</p><pre>public class Blog {<br>    public int Id { get; set; }<br>    public string Name { get; set; }<br>    public List&lt;Post&gt; Posts { get; set; }<br>}<br><br>public class Post {<br>    public int Id { get; set; }<br>    public string Title { get; set; }<br>    public int BlogId { get; set; }<br>}</pre><p>Now, look at this innocent-looking code. We want to print the title of every post for every blog.</p><pre>// 1 query to the database to get all blogs<br>var blogs = _context.Blogs.ToList(); <br><br>foreach (var blog in blogs)<br>{<br>    // DANGER: This line sends a new SQL query to the database <br>    // for EVERY single blog in the loop!<br>    foreach (var post in blog.Posts) <br>    {<br>        Console.WriteLine($&quot;Blog: {blog.Name}, Post: {post.Title}&quot;);<br>    }<br>}</pre><p>If you have 100 blogs, this code will hit your database 101 times. One time to get the blogs, and 100 separate times to get the posts for each blog.</p><p><strong>What the Database Sees</strong></p><pre>-- The &quot;1&quot; Query: Fetches all blogs<br>SELECT [b].[Id], [b].[Name] FROM [Blogs] AS [b];<br><br>-- The &quot;N&quot; Queries: One query for EACH blog to get its posts<br>SELECT [p].[Id], [p].[Title], [p].[BlogId] FROM [Posts] AS [p] WHERE [p].[BlogId] = 1;<br>SELECT [p].[Id], [p].[Title], [p].[BlogId] FROM [Posts] AS [p] WHERE [p].[BlogId] = 2;<br>SELECT [p].[Id], [p].[Title], [p].[BlogId] FROM [Posts] AS [p] WHERE [p].[BlogId] = 3;<br>-- ... and this repeats for every single blog!</pre><p>Instead of bringing all the data home in a single, efficient trip, the application is constantly chatting with the database server over the network. This network latency is what causes the app to crawl to a halt.</p><p><strong>How to Fix the N+1 Problem in EF Core</strong></p><p>Fortunately, fixing this is incredibly simple. You want to tell EF Core to fetch the blogs and their posts together in a single database roundtrip.<br>Here are two best ways to do it:</p><ol><li>Eager Loading(Using <strong>.Include</strong>)</li></ol><p>The most common fix is to use the <strong>.Include()</strong> method. This tells EF Core to generate a standard SQL <strong>LEFT JOIN </strong>and load the related data right away.</p><pre>// This fires exactly ONE query to fetch blogs and their posts<br>var blogs = _context.Blogs<br>                    .Include(b =&gt; b.Posts)<br>                    .ToList(); <br><br>foreach (var blog in blogs)<br>{<br>    // No extra queries are fired here! The data is already loaded.<br>    foreach (var post in blog.Posts) <br>    {<br>        Console.WriteLine($&quot;Blog: {blog.Name}, Post: {post.Title}&quot;);<br>    }<br>}</pre><p>2. Projection(Using <strong>.Select</strong>)</p><p>If you don’t need the entire <strong>Blog </strong>or <strong>Post </strong>objects, projecting the data into a custom shape using <strong>.Select() </strong>is even fater. It only pulls the exact columns you ask for.</p><pre>// This also fires exactly ONE highly optimized query<br>var blogData = _context.Blogs<br>                       .Select(b =&gt; new {<br>                           BlogName = b.Name,<br>                           PostTitles = b.Posts.Select(p =&gt; p.Title).ToList()<br>                       })<br>                       .ToList();</pre><p><strong>Catch It Before Production</strong></p><p>The N+1 query problem is a silent performance killer because it hides in completely innocent-looking C# code. It works perfectly on your local machine with a tiny test database, but it will bring your database to its knees the moment it hits production with thousands of records.</p><p><strong>Golden Rule</strong></p><p>Always be mindful of loops and data relationship in EF Core. If you need related data, use <strong>.Include() </strong>or <strong>.Select() </strong>to fetch everything in a single, efficient database trip.</p><p><strong>Pro Tip to prevent this</strong></p><p>Turn on EF Core logging in your development environment using <strong>LogTo(Console.WriteLine). </strong>By watching your console output, you can easily spot if a single page load is spamming the database with repeatitive <strong>SELECT </strong>queries.</p><p>Catching this simple mistake early is the difference between a sluggish, frustrating app and a lightning-fast, production-ready system.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba44fbf156e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>