<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth" -->

---
title: We Fixed N+1 With Eager Loading. Memory Usage Tripled.
description: A production incident walkthrough describes how eager loading was used to fix an N+1 query problem, cutting query count from 801 to one, but causing container...
canonical: https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: We Fixed N+1 With Eager Loading. Memory Usage Tripled. | daily.dev
og:description: A production incident walkthrough describes how eager loading was used to fix an N+1 query problem, cutting query count from 801 to one, but causing container...
og:url: https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth
og:image: https://api.daily.dev/og/posts/4OqpYkzTH.png
og:image:alt: We Fixed N+1 With Eager Loading. Memory Usage Tripled.
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# We Fixed N+1 With Eager Loading. Memory Usage Tripled.

**[Medium](https://daily.dev/sources/medium_js)** · 5 min read · 54 upvotes · 7 comments

## Summary

A production incident walkthrough describes how eager loading was used to fix an N+1 query problem, cutting query count from 801 to one, but causing container memory to jump from 420 MB to 1.3 GB and pods to be killed under load. The root cause was a Cartesian multiplication effect from joining multiple one-to-many relationships, which expanded a single order into 72 repeated rows. The fix involved paginating orders, using bounded IN queries per page, selecting only needed fields, and computing summaries in SQL rather than materializing full object graphs. The team added memory budgets, response-size guards, and histograms of children-per-parent to catch this class of issue earlier, concluding that query count alone is a poor performance proxy.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://ai.plainenglish.io/we-fixed-n-1-with-eager-loading-memory-usage-tripled-d31ea4661bfd>

## Questions this post answers

### why did fixing an N+1 query problem with eager loading cause memory usage to triple in production

Eager loading replaced 801 small queries with one large joined query, but joining multiple one-to-many relationships created a Cartesian multiplication effect where an order with 12 line items, 3 discounts, and 2 shipments could appear 72 times in the result set. The ORM then had to reassemble those repeated rows into objects, holding database results, an ORM object graph, a response graph, and a serialized JSON buffer simultaneously, pushing container memory from 420 MB to 1.3 GB and killing pods under normal traffic.

_daily.dev surfaces war stories like this for engineers debugging ORM memory blowups after an eager-loading fix._

### how do you fix N+1 queries without causing memory blowups from eager loading joins

Split the load along relationship boundaries instead of using one giant join: fetch parent records in bounded pages, load related collections with separate IN queries per page to avoid Cartesian multiplication between independent one-to-many relationships, select only fields the client uses, and compute summaries in SQL rather than materializing full child collections. This keeps query count low while giving each query a predictable upper bound on rows and memory.

_track this kind of bounded-loading pattern on daily.dev when weighing ORM query strategies against memory risk._

## Community discussion

Top comments from developers on daily.dev.

**@xendk** · 3 upvotes

> An awfull lot of big words to cover the fact that if you use join to pull in a lot of one-to-many releations, you end up repeating your data and use more space.
>
> I object to the opening line: "The optimization looked perfect in the pull request.". No, it looked good because nobody bothered to think.

**@trevorsuna** · 2 upvotes

> Query count is a tempting score because it is easy to measure, but the 72-row Cartesian expansion shows why it is incomplete. Bounded IN queries, narrow projections, and SQL-side summaries trade one elegant ORM graph for predictable memory, which is usually the better production bargain.

**@ajaychoudhary80** · 2 upvotes

> Great reminder that query count alone is a poor measure of database performance—memory and result-set size matter just as much.

**@etiene** · 0 upvotes

> How were they able to achieve that response size guard? I'm interested in the thinking process behind that.

**@jenueldev** · 0 upvotes

> The key shift here is treating fan-out as a bounded resource, not assuming fewer queries means less work. The children-per-parent histogram seems especially valuable because it exposes data-shape drift that average latency can hide until one tenant hits pathological cardinality.

---

Tags: [#performance](https://daily.dev/tags/performance), [#database](https://daily.dev/tags/database)

[View this post on daily.dev](https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"We Fixed N+1 With Eager Loading. Memory Usage Tripled.","url":"https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth"},"datePublished":"2026-09-04T22:35:29.394Z","dateModified":"2026-09-14T09:11:37.206Z","description":"A production incident walkthrough describes how eager loading was used to fix an N+1 query problem, cutting query count from 801 to one, but causing container...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/05522a30f425851d928dc91d7a51240f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/05522a30f425851d928dc91d7a51240f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":7,"discussionUrl":"https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":54},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":7}],"keywords":"performance,database","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"We Fixed N+1 With Eager Loading. Memory Usage Tripled."}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth","comment":[{"@type":"Comment","text":"An awfull lot of big words to cover the fact that if you use join to pull in a lot of one-to-many releations, you end up repeating your data and use more space.\nI object to the opening line: “The optimization looked perfect in the pull request.”. No, it looked good because nobody bothered to think.","datePublished":"2026-09-06T18:48:07.878Z","url":"https://daily.dev/posts/4OqpYkzTH#c-a587xQSMz","author":{"@type":"Person","name":"Thomas Fini Hansen","url":"https://daily.dev/xendk"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3}},{"@type":"Comment","text":"Query count is a tempting score because it is easy to measure, but the 72-row Cartesian expansion shows why it is incomplete. Bounded IN queries, narrow projections, and SQL-side summaries trade one elegant ORM graph for predictable memory, which is usually the better production bargain.","datePublished":"2026-09-06T01:59:52.349Z","url":"https://daily.dev/posts/4OqpYkzTH#c-sA5hnGvrN","author":{"@type":"Person","name":"Trevor Suna","url":"https://daily.dev/trevorsuna","image":"https://media.daily.dev/image/upload/s--dZ7gXxpp--/f_auto/v1784081551/avatars/avatar_EMoP47rpuw8DNjhp6R1b6?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"Great reminder that query count alone is a poor measure of database performance—memory and result-set size matter just as much.","datePublished":"2026-09-08T01:21:02.774Z","url":"https://daily.dev/posts/4OqpYkzTH#c-QZJjunKxU","author":{"@type":"Person","name":"Ajay Choudhary","url":"https://daily.dev/ajaychoudhary80","image":"https://media.daily.dev/image/upload/s--WkX8wDW2--/f_auto/v1780989706/avatars/avatar_QNZlI2SgVnNSx5PSRpCVx?_a=BAMAMiWQ0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"How were they able to achieve that response size guard? I’m interested in the thinking process behind that.","datePublished":"2026-09-05T07:36:19.240Z","url":"https://daily.dev/posts/4OqpYkzTH#c-y6LjkSnWT","author":{"@type":"Person","name":"Etiene James","url":"https://daily.dev/etiene","image":"https://media.daily.dev/image/upload/v1675164890/avatars/avatar_yp08BYiIBvg8bT1Q0D2ZC.jpg"}},{"@type":"Comment","text":"The key shift here is treating fan-out as a bounded resource, not assuming fewer queries means less work. The children-per-parent histogram seems especially valuable because it exposes data-shape drift that average latency can hide until one tenant hits pathological cardinality.","datePublished":"2026-09-07T03:37:25.334Z","url":"https://daily.dev/posts/4OqpYkzTH#c-kQVFE18B0","author":{"@type":"Person","name":"Jenuel Oras Ganawed","url":"https://daily.dev/jenueldev","image":"https://media.daily.dev/image/upload/s--BadmiNyj--/f_auto/v1787625077/avatars/avatar_JJtZr4Sjm?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/we-fixed-n-1-with-eager-loading-memory-usage-tripled--4oqpykzth#faq","mainEntity":[{"@type":"Question","name":"why did fixing an N+1 query problem with eager loading cause memory usage to triple in production","acceptedAnswer":{"@type":"Answer","text":"Eager loading replaced 801 small queries with one large joined query, but joining multiple one-to-many relationships created a Cartesian multiplication effect where an order with 12 line items, 3 discounts, and 2 shipments could appear 72 times in the result set. The ORM then had to reassemble those repeated rows into objects, holding database results, an ORM object graph, a response graph, and a serialized JSON buffer simultaneously, pushing container memory from 420 MB to 1.3 GB and killing pods under normal traffic. daily.dev surfaces war stories like this for engineers debugging ORM memory blowups after an eager-loading fix."}},{"@type":"Question","name":"how do you fix N+1 queries without causing memory blowups from eager loading joins","acceptedAnswer":{"@type":"Answer","text":"Split the load along relationship boundaries instead of using one giant join: fetch parent records in bounded pages, load related collections with separate IN queries per page to avoid Cartesian multiplication between independent one-to-many relationships, select only fields the client uses, and compute summaries in SQL rather than materializing full child collections. This keeps query count low while giving each query a predictable upper bound on rows and memory. track this kind of bounded-loading pattern on daily.dev when weighing ORM query strategies against memory risk."}}]}
```

