What is the N+1 query problem, and how do you fix it in an ORM?
Reviewed by Gurusankar M.
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
-- 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
- JOIN FETCH vs @BatchSize vs entity graph — trade-offs?
- Why can JOIN FETCH with pagination be dangerous?
- 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.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair