Database Access

ORM, query builder or raw SQL as an engineering decision with consequences, plus the query patterns and pool limits that decide how a backend behaves under load.

Choosing a Data Access Layer

ORM, query builder, raw SQL and stored procedures as four points on a spectrum, chosen per query rather than per project.

Q · ORM, query builder, raw SQL or stored procedure — which one, and on what evidence?
What an ORM Actually Does

Object method to generated SQL to database, plus the identity map, unit of work and lazy proxies that decide when statements are issued.

Q · When I call a method on an object, what SQL runs, and when?
What an ORM Buys and What It Costs

An honest ledger: real productivity on entity-shaped work, real opacity on query count, plans and complex reads.

Q · What do I actually gain from an ORM, and what am I paying for it?
The N+1 Query Problem
▶ lab

One query for the list, one more for every row: 100 users become 101 statements, and the source code shows none of it.

Q · Why does an endpoint that reads one table issue a hundred queries, and how do I see it before production does?
Eager Loading and Batching

The two general fixes for per-row queries — load the relation up front, or collect the ids and fetch once — and what each one over-fetches.

Q · How do I load related data in a bounded number of queries without fetching things nobody asked for?
Query Builders

Composing SQL structurally in the host language: dynamic filters without string concatenation, and no mapping layer to explain.

Q · How do I build a query whose shape depends on runtime input without concatenating strings?
Raw SQL in Application Code

When to write the statement yourself, how to keep it parameterized and findable, and what you take on when you do.

Q · When is hand-written SQL the right call, and how do I keep it from becoming an unmaintainable pile of strings?
Schema Migrations from the Application Side

Schema change as a deployment problem: two code versions run at once, and some `ALTER TABLE` statements take a lock that stops the service.

Q · How do I change the schema of a database that a running service is using right now?
Connection Pools
▶ lab

The pool is what actually bounds your concurrency: 1,000 requests against 20 connections means 20 running and 980 waiting, silently, until they time out.

Q · How many database queries can my service really run at once, and what happens to the rest?