MediumExperience level: 3-5 years · 8-15 years2 min read

What is the N+1 query problem, and how do you fix it in an ORM?

Reviewed by Gurusankar M.

Asked inAmazonDeloitteCognizantAccenture
#n+1#orm#hibernate#lazy loading#join fetch#batch
Report issue

Short Answer

N+1 = one query loads N parents, then the ORM fires one more query per parent to load its children (1 + N). Fix with a JOIN FETCH / eager join, an IN-clause batch fetch (@BatchSize), or an entity graph — turning N+1 round-trips into 1–2 queries.

On this page

Concept Made Simple

Coffee Chat Question

“What is the N+1 query problem, and how do you fix it in an ORM?”

Remember It Faster

Mind Map Answer

Loop over 100 orders, touch order.getItems() → 1 query for orders + 100 lazy queries for items = 101 round-trips. Fetch the children in the same query instead.

Learn by Doing

Hands-on Keyboard

sql
-- N+1 (lazy): SELECT * FROM orders;  then per order:
--             SELECT * FROM items WHERE order_id = ?
-- Fix: one query
SELECT * FROM orders o JOIN items i ON i.order_id = o.id;

Think Beyond the Expected

What If?

An endpoint is fast in dev (10 rows) but times out in prod (10k rows) — likely cause?

Classic N+1: it scales with row count, so it's invisible on small dev data but explodes in prod. Detect it by logging SQL counts per request, then fix with JOIN FETCH / batch fetching / entity graph.

Real World

N+1 is the #1 ORM performance bug. Teams catch it by asserting query counts in tests and watching SQL logs; the cure is fetch-joins or batch sizing, not turning off lazy loading globally.

Interviewer's Expectation

Keywords they're listening for:

  • 1 + N queries
  • lazy loading cause
  • JOIN FETCH / @BatchSize / entity graph
  • scales with row count
  • detect via query count

Common Mistakes

  • Eager-loading everything to 'fix' N+1 (over-fetch)
  • Not noticing N+1 until prod load
  • JOIN FETCH a collection with LIMIT (in-memory pagination)

Best Practices

  • Fetch what you need with JOIN FETCH / entity graphs
  • Use @BatchSize for collections
  • Assert SQL query counts in integration tests

Follow-up Questions

  1. JOIN FETCH vs @BatchSize vs entity graph — trade-offs?
  2. Why can JOIN FETCH with pagination be dangerous?
  3. How do you assert query count in tests?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Optimization (SQL)
Interview question: "What is the N+1 query problem, and how do you fix it in an ORM?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.