# The N+1 query problem in plain words

> One query fetches a list, then one more query runs for every item in it. Why it hides so well in development, how to spot it in the logs, and the two standard fixes.

Source: https://hintvo.com/blog/n-plus-one-queries-in-plain-words | Last updated: November 20, 2026 | Reviewed: 2026-11-20

The N+1 problem is when code runs one query to fetch a list, then one more query for every item in that list. A page showing fifty orders runs fifty-one queries where two would do. It is one of the most common reasons a page is slow.

## What it looks like

```ts
const orders = await db.orders.findMany({ where: { status: "open" } }); // 1 query

for (const order of orders) {
  order.customer = await db.customers.findById(order.customerId);       // N queries
}
```

## Why nobody notices in development

On a laptop the test database has five orders, so the page runs six queries and feels instant. In production the list has five hundred, each query adds a trip to the database, and the page takes seconds. The code did not change. The data did.

## How to spot it

- The query log shows the same query many times in a row, with only the id changing.
- The number of queries on a page grows with the number of items shown.
- A page that was fast last year is slow now, and nobody touched it.

## The two fixes

- **Fetch them together.** Collect the ids, then load all the customers in one query.
- **Join.** Ask the database for the orders with their customers in a single query.

```ts
const orders = await db.orders.findMany({ where: { status: "open" } });
const ids = orders.map((o) => o.customerId);
const customers = await db.customers.findMany({ where: { id: { in: ids } } });
// 2 queries, whatever N is
```

Most data libraries have a built-in way to ask for related records up front. It is worth knowing what yours calls it.

## Where it hides

- In templates and serializers that reach for a related record.
- In helper functions that look harmless and are called in a loop.
- In APIs that return nested data.

## Questions you will hear in a meeting

- How many queries does this page run?
- Does that number change with the size of the list?
- Are we loading the related records up front?

## In an interview

If you write a loop with a database call inside it during a coding round, say so before the interviewer does, and say how you would batch it. Noticing it yourself is worth more than never writing it.

> **Where Hintvo fits:** when a term like this comes up in a call, the In plain words card explains it in one sentence, in the context of your project. When someone asks how your own code handles it, the answer names the file and line.

## Related

- [Database indexes in plain words](https://hintvo.com/blog/database-indexes-in-plain-words.md): What is a database index, in plain words?
- [How to walk a team through your pull request](https://hintvo.com/blog/walk-a-team-through-your-pull-request.md): How do I present a pull request or a code change to my team?
- [Coding interviews: what to do in the first five minutes](https://hintvo.com/blog/coding-interview-first-five-minutes.md): What should I do in the first five minutes of a coding interview?
