Skip to content

N+1 queries and slow resolvers in GraphQL

GraphQL N+1 query elimination with DataLoader

Stop GraphQL resolvers from firing one query per item: batch with DataLoader, join where it's cheaper, and cap query depth and complexity.

Symptoms

Signs your platform has this problem

If several of these sound familiar, the plan below is where we would start.

01

One request, hundreds of queries

Nested field resolvers running a separate SELECT for each item in a list.

02

Database CPU spikes on nested queries

A simple author-and-posts query flooding the database with round trips.

03

Slow API responses

Round trips between the application and the database adding up to seconds.

Remediation plan

How we fix it, step by step

Each phase ends with a measurement, so you can see what changed before the next one starts.

01

Resolver profiling

Logging SQL queries per GraphQL operation to find N+1 hot spots.

02

DataLoader batching

Per-request DataLoaders that combine individual IDs into a single SELECT ... WHERE id IN (...) query.

03

Lookahead joins

Reading the requested fields from resolve info to fetch nested data with one SQL join.

04

Depth and complexity limits

Rejecting deeply nested or expensive queries before execution, for example with graphql-armor.

Technical checklist

Remediation checklist

What we check before a change goes to production:

  • Add per-request DataLoader batching for relational resolvers
  • Replace per-item nested queries with WHERE id IN (...) batches
  • Set query depth and complexity limits
  • Assert query counts per operation in integration tests

What we measure

We take a baseline first and report the same measurements after each change, from your own monitoring — evidence, not promised results.

Queries per request
SQL queries per GraphQL operation, from query logs
p95 latency
Key operations, before and after
Database CPU
Primary CPU during peak GraphQL traffic

Related service

Custom software

Custom software for Indian SMEs and startups, built around your workflow with an agreed scope, review milestones, clear ownership and documented handover.

Explore Custom software

Questions

Questions about this remediation

When a query asks for a list of N items and a related field on each, naive resolvers run one query for the list plus one per item: N+1 database round trips.

DataLoader collects the IDs requested by resolvers in the same tick of the event loop and fetches them with one batched query, caching results for the rest of the request.

Want an engineer to look at this with you?

Send us the symptoms and any metrics you have. We'll reply within one business day, set up a call and agree what to measure before anything changes.