Data, ORMs and caching · mid
ORMs, migrations and the N+1 problem
An ORM like Prisma or Drizzle maps tables to typed objects and generates SQL, but loading related rows inside a loop silently sends one query per row (N+1), so you load relations in one batched query or a join, and you change the schema only through versioned migrations.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- N+1: one query for the list, then one per item for its relation (100 posts -> 101 queries). Each query is a network round trip, so latency explodes.
- Fixes: include/with relations (Prisma's include runs one extra query with IN; relationLoadStrategy: 'join' uses a database join), fetch related rows with WHERE id IN (...) and group in code, or batch with a DataLoader.
- Prisma: schema file + generated typed client; Prisma 7 (Nov 2025) removed the Rust engine by default and needs prisma.config.ts and driver adapters; Prisma 8, a rebuild, is in release candidates in late 2026. Drizzle: SQL-like TypeScript query builder; 0.45 is stable and 1.0 is in release candidates.
- Look at the SQL your ORM sends (query logging) and drop to raw, parameterised SQL for complex reports.
- Migrations: versioned files (prisma migrate, drizzle-kit) committed to git and run in CI/CD, never hand-edited in production.
- Zero-downtime changes use expand and contract: add the new column, write to both, backfill, switch reads, then drop the old column in a later deploy.
- Create one ORM client per process and reuse it; creating clients per request exhausts connections.
The red pen
Common traps: mistake → fix
Mistake: The ORM makes queries efficient automatically
Fix: lazy relation loading in loops is the classic N+1.
Mistake: Rename a column in one migration while the old code is still running
Fix: old instances break during the deploy; use expand and contract.
Mistake: ORMs prevent all SQL injection
Fix: raw query helpers with string concatenation are still injectable.
Easy to hard
Questions interviewers ask, easy to hard
- Which ORM have you used? What does it give you?
- What is the N+1 problem? How do you spot it and fix it?
- How do you run schema migrations safely with zero downtime?
- When do you skip the ORM and write SQL?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.