Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 6 OF 6 · 2 MIN READ

ORMs, the N+1 Problem and Connection Pooling

ORMs such as Prisma, Drizzle, TypeORM and Sequelize speed up development but hide the SQL they run. The N+1 query problem and connection pool exhaustion are the two ORM issues interviewers ask about most.

What you will learn

  • Spot and fix N+1 queries
  • Use eager loading, joins and batching
  • Size a connection pool and know why it matters
  • Run migrations safely

Key concepts

N+1 problem

One query loads N parent rows, then the code runs one more query per row for its children: 1 + N round trips. With 100 posts that is 101 queries instead of 1 or 2.

Eager loading

Ask the ORM to load relations together, with a JOIN or a second WHERE id IN (...) query (Prisma include, Sequelize include).

Connection pool

A fixed set of open database connections reused across requests. Opening a connection per request is slow, and too many connections overload the database. Serverless functions often need an external pooler such as PgBouncer.

Migrations

Versioned schema changes in code, applied in order. Make them backward compatible (expand, migrate, contract) so old and new app versions can run during a deploy.

Step by step

From N+1 to two queries

// N+1: one query for posts, then one per post
const posts = await prisma.post.findMany({ take: 50 })
for (const p of posts) p.author = await prisma.user.findUnique({ where: { id: p.authorId } })

// Fixed: Prisma batches the relation into one extra query
const postsWithAuthors = await prisma.post.findMany({ take: 50, include: { author: { select: { id: true, name: true } } } })

Safe column rename (expand / contract)

  • Add the new column
  • Write to both, backfill old rows
  • Switch reads to the new column
  • Stop writing the old one and drop it in a later release

Examples

Seeing the queries the ORM runs

const prisma = new PrismaClient({ log: ['query'] })
// or in PostgreSQL: log_min_duration_statement = 200  -- log every query slower than 200 ms

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Find an N+1 in a GraphQL resolver and fix it with DataLoader.
  • Explain what happens when 200 requests hit an API with a pool of 10 connections.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

ORM or raw SQL?

An ORM or query builder for most CRUD, with type safety and migrations; raw SQL for complex reports, bulk operations and performance-critical queries. Always look at the SQL that runs.

How big should a connection pool be?

Smaller than people expect. The database works best with roughly a few connections per CPU core. Total connections across all app instances must stay under the database limit; extra requests wait in the pool queue.

Summary

Watch for N+1 queries and fix them with eager loading or batching, share a right-sized connection pool, log slow queries, and write backward-compatible migrations.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus