<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/FFCsnZgIb" -->

---
title: Day 9/30 — Kill the N+1 Query | daily.dev
description: The N+1 query problem occurs when an endpoint fetches a list of records and then issues a separate database query for each related record — turning one request...
canonical: https://daily.dev/posts/day-9-30-kill-the-n-1-query-ffcsnzgib
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Day 9/30 — Kill the N+1 Query | daily.dev
og:description: The N+1 query problem occurs when an endpoint fetches a list of records and then issues a separate database query for each related record — turning one request...
og:url: https://daily.dev/posts/day-9-30-kill-the-n-1-query-ffcsnzgib
og:image: https://api.daily.dev/og/posts/FFCsnZgIb.png
og:image:alt: Day 9/30 — Kill the N+1 Query
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.

# Day 9/30 — Kill the N+1 Query

**[Nestjs Developers](https://daily.dev/sources/nestjsdevs)** · [@ezilemdodana](https://daily.dev/ezilemdodana) · 1 min read · 137 upvotes · 17 comments

## Summary

The N+1 query problem occurs when an endpoint fetches a list of records and then issues a separate database query for each related record — turning one request into dozens or hundreds of queries. ORMs mask this because the code looks clean. The fix is to use query logs and traces to measure actual query counts, then load relationships deliberately using joins, relation loaders, or batched queries.

## Content

Your endpoint returns 100 orders.

It then fetches the customer for every order.

Congratulations: one innocent endpoint just created 101 database queries.

The N+1 problem hides well in ORMs because the code looks clean while the database performs repetitive work.

Use query logs and traces. Load relationships deliberately with joins, relation loaders or batched queries.

Performance starts with knowing how many queries one request actually executes.

What is the highest query count you have found behind one endpoint?

#NestJS #TypeORM #DatabasePerformance

## Community discussion

Top comments from developers on daily.dev.

**@arlangalvez21** · 4 upvotes

> The code needs to be checked. Most of the iterative work to fetch data can be done with nested joins. Also, you can use the
>
> ```
> SELECT x FROM y WHERE y.id IN(id1,id2,...)
> ```

**@mklead** · 2 upvotes

> This is vague, add some script next time...
> For SQL, usually joins solve this. Don't just post for attention.

**@mohamedtarektah** · 2 upvotes

> 7M + 1 in one query

**@ahmetozel** · 2 upvotes

> Query count is a great regression target, not just a debugging observation. I would add an integration test that loads 1, 10, and 100 orders and asserts the number of database round trips stays bounded rather than growing linearly. Joins are not automatically free, though: one-to-many eager loading can multiply rows and break pagination. A batched loader plus per-request query tracing often gives a safer balance between round trips, memory, and result shape.

**@agustinbarrientos** · 2 upvotes

> Put query-count traces at the serializer boundary, where an efficient repository call can quietly become one round trip per nested field

## Similar posts on daily.dev

- [Understanding the N\+1 Problem in GraphQL \(and Why It’s Not Just GraphQL\)](https://daily.dev/posts/understanding-the-n-1-problem-in-graphql-and-why-it-s-not-just-graphql--ub9yybdyf) · WunderGraph · 0 upvotes · 0 comments

---

Tags: [#nodejs](https://daily.dev/tags/nodejs), [#nestjs](https://daily.dev/tags/nestjs)

[View this post on daily.dev](https://daily.dev/posts/day-9-30-kill-the-n-1-query-ffcsnzgib)

```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":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/day-9-30-kill-the-n-1-query-ffcsnzgib","headline":"Day 9/30 — Kill the N+1 Query","text":"The N+1 query problem occurs when an endpoint fetches a list of records and then issues a separate database query for each related record — turning one request into dozens or hundreds of queries. ORMs mask this because the code looks clean. The fix is to use query logs and traces to measure actual query counts, then load relationships deliberately using joins, relation loaders, or batched queries.","url":"https://daily.dev/posts/day-9-30-kill-the-n-1-query-ffcsnzgib","datePublished":"2026-08-05T09:45:53.126Z","dateModified":"2026-08-05T09:46:10.586Z","author":{"@type":"Person","name":"Ezile Mdodana","url":"https://daily.dev/ezilemdodana","image":"https://media.daily.dev/image/upload/s--1OI8gwnO--/f_auto/v1771923757/avatars/avatar_7vB9SeLRKhOHGnzEpmFvQ?_a=BAMAMiiu0","description":"Software Project Lead sharing practical insights on NestJS, Angular, AWS and scalable systems.","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":8430}},"image":"https://media.daily.dev/image/upload/s--Md_phsIq--/f_auto/v1785923154/posts/FFCsnZgIb?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":137},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":17}],"comment":[{"@type":"Comment","text":"The code needs to be checked. Most of the iterative work to fetch data can be done with nested joins. Also, you can use the\nSELECT x FROM y WHERE y.id IN(id1,id2,...)","datePublished":"2026-08-05T15:33:40.160Z","url":"https://daily.dev/posts/FFCsnZgIb#c-mKHWy7x8U","author":{"@type":"Person","name":"Arlan Galvez","url":"https://daily.dev/arlangalvez21","image":"https://lh3.googleusercontent.com/a/ACg8ocJUS7uJkW5cquK3R9j4Blo6PpRTJpZSd-05hU6KLbMr6w=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":4}},{"@type":"Comment","text":"This is vague, add some script next time…\nFor SQL, usually joins solve this. Don’t just post for attention.","datePublished":"2026-08-06T06:25:13.309Z","url":"https://daily.dev/posts/FFCsnZgIb#c-wkYg6xOax","author":{"@type":"Person","name":"Marvin","url":"https://daily.dev/mklead","image":"https://lh3.googleusercontent.com/a/ACg8ocISrottnjmNOVrGcXn6Ewmz9psfNHuLl5S5vby4bUBOyQZETA=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"7M + 1 in one query","datePublished":"2026-08-05T15:23:27.316Z","url":"https://daily.dev/posts/FFCsnZgIb#c-UkwJeOX1D","author":{"@type":"Person","name":"Mohamed Tarek","url":"https://daily.dev/mohamedtarektah","image":"https://avatars.githubusercontent.com/u/191804127?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"Query count is a great regression target, not just a debugging observation. I would add an integration test that loads 1, 10, and 100 orders and asserts the number of database round trips stays bounded rather than growing linearly. Joins are not automatically free, though: one-to-many eager loading can multiply rows and break pagination. A batched loader plus per-request query tracing often gives a safer balance between round trips, memory, and result shape.","datePublished":"2026-08-05T17:36:29.157Z","url":"https://daily.dev/posts/FFCsnZgIb#c-f5QOyJFvR","author":{"@type":"Person","name":"Ahmet Özel","url":"https://daily.dev/ahmetozel","image":"https://avatars.githubusercontent.com/u/70992231?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"Put query-count traces at the serializer boundary, where an efficient repository call can quietly become one round trip per nested field","datePublished":"2026-08-06T17:29:20.980Z","url":"https://daily.dev/posts/FFCsnZgIb#c-QxJPf5q5Y","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/nestjsdevs","name":"Nestjs Developers"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Nestjs Developers","item":"https://daily.dev/squads/nestjsdevs"},{"@type":"ListItem","position":3,"name":"Day 9/30 — Kill the N+1 Query"}]}
```

