N+1 queries: the hidden reason your dashboard is slow
What N+1 queries are, how ORMs and React components create them without anyone noticing, how to find them, and four ways to fix them.
Samed Škulj
· 3 min read
A dashboard that loads instantly with ten rows and takes five seconds with five hundred almost always has the same problem: N+1 queries. It's the most common database issue I find in SaaS apps, and one of the easiest to fix once you see it.
What it is
You load a list (1 query), then for each item in the list you load something related (N queries). Fifty bookings, each with its customer, becomes 51 queries.
const bookings = await db.booking.findMany({ where: { organizationId } });
for (const booking of bookings) {
booking.customer = await db.customer.findUnique({
where: { id: booking.customerId },
});
}
Each query might take only 5 ms, but they run one after another, and each one pays network latency to the database. With more rows, the page gets linearly slower.
How it hides
N+1 rarely looks like the loop above. More often it's:
- An ORM loading relations lazily, so accessing
booking.customerin a template triggers a query. - A React component that fetches its own data, rendered once per row. Each
<BookingRow>calls an API for its customer. - A GraphQL resolver that loads a related object for each item in a list.
- An API that returns IDs, so the frontend calls another endpoint for each one.
With small test data, it's invisible. With a real customer's data, it's the slowest page in the app.
How to find it
- Log queries in development with timing. Load the slow page and count. If the number of queries grows with the number of rows, you've found it.
- Look at the network tab for the browser version: dozens of near-identical API calls.
- Use your APM or database insights: the same query shape executed hundreds of times per request.
- In Postgres,
pg_stat_statementsshows which queries run most often in total.
Four ways to fix it
1. Join or include in one query
Let the database do the work:
const bookings = await db.booking.findMany({
where: { organizationId },
include: { customer: true },
});
Or in SQL:
select b.*, c.name as customer_name
from bookings b
join customers c on c.id = b.customer_id
where b.organization_id = $1
order by b.starts_at desc
limit 50;
2. Batch load by IDs
When a join doesn't fit, collect the IDs and load them in one query:
const ids = [...new Set(bookings.map((b) => b.customerId))];
const customers = await db.customer.findMany({ where: { id: { in: ids } } });
const byId = new Map(customers.map((c) => [c.id, c]));
Two queries, regardless of row count.
3. Use a DataLoader in GraphQL or resolvers
A DataLoader collects all the lookups made in one tick and turns them into a single batched query. It's the standard fix for resolver-shaped N+1.
4. Fetch once at the top, pass data down
In React, fetch the list with its related data in a Server Component or a single API call, and pass it to row components as props. Rows shouldn't fetch their own data.
Then check the indexes
Once the query count is down, make sure the remaining queries are fast. The organization_id filter and the starts_at sort above need an index to stay fast as data grows. See Postgres indexes every SaaS app needs.
Add pagination
Even a perfect query is slow if it returns 10,000 rows. Paginate lists, and prefer cursor-based pagination for large or frequently updated tables.
The N+1 dashboard is finding #5 in the sample audit report for a reason: it's in almost every app. A performance and code audit finds them across your whole app, ranked by how much time they cost your users.

Written by Samed Škulj
Senior Full-Stack Engineer since 2021. I have shipped with teams at Deel, HTEC Group (for Zoox), Ministry of Programming, and now help SaaS teams add AI features and get AI-built apps ready for production. LinkedIn