SQL & Database Expert
Think in sets, not loops. The database is smarter than your application loop. When something is slow, read the plan (EXPLAIN ANALYZE) before guessing — the answer is almost always a missing index or a bad join.
When to Use
- Writing, debugging, or optimizing SQL.
- Schema design: tables, relationships, constraints, types, migrations.
- Slow queries, missing/unused indexes, N+1, lock contention.
- Transactions, isolation levels, and concurrency correctness.
When NOT to Use
- ORM/app wiring →
nodejs-backend-expert / language skill.
- Vector/semantic retrieval →
rag-expert.
- Data-platform / warehouse-scale architecture →
software-architect.
Core Principles
1. Schema design
- Normalize to 3NF by default; denormalize only for a measured read win, and then keep it consistent (triggers/jobs).
- Right types:
timestamptz (not naive timestamps), numeric/decimal for money (never float), uuid/bigint keys, native enum/boolean. Enforce integrity at the DB: NOT NULL, UNIQUE, FOREIGN KEY, CHECK — the app is not the only writer.
2. Query like a pro
- Select only needed columns (no
SELECT * in app queries — it breaks covering indexes and over-fetches).
- Know your JOINs and beware fan-out: a one-to-many join multiplies rows and inflates
SUM/COUNT. Pre-aggregate in a subquery/CTE.
- Kill N+1 patterns: one query with a JOIN or
WHERE id IN (...) instead of a query per row.
EXISTS over IN (subquery) for correlated existence checks; window functions for running totals/ranking instead of self-joins.
3. Indexing
- Index columns in
WHERE, JOIN, and ORDER BY. Composite index order: equality columns first, then the range/sort column. The index serves a left-to-right prefix.
- A function/expression on the column (
WHERE lower(email)=…, WHERE created_at::date=…) defeats a plain index — index the expression or rewrite as a sargable range.
- Indexes speed reads, slow writes, and use space — add deliberately. Drop unused ones. Use partial/covering indexes for hot queries.
4. Read the plan & transactions
EXPLAIN (ANALYZE, BUFFERS). Red flags: seq scan on a large table for a selective filter, row-estimate vs actual far off (stale stats — ANALYZE), nested-loop over huge sets.
- Keep transactions short; choose isolation deliberately (
READ COMMITTED default; SERIALIZABLE for invariants, with retry on serialization failure). Acquire locks in a consistent order to avoid deadlocks.
Common Mistakes
SELECT * in application code → over-fetch, fragile, no covering index.
- Aggregates over a fanned-out JOIN → doubled sums; aggregate before joining.
- Functions on indexed columns in
WHERE → full scan.
- Wrong composite-index column order → index unused for the query.
- Offset pagination on huge tables (
OFFSET 100000) → slow; use keyset/cursor pagination.
- Floating-point money → rounding errors; use
numeric.
- Implicit type mismatch in joins/filters → silent full scans.
Examples
Avoid JOIN fan-out with a CTE; keyset pagination
WITH totals AS (
SELECT order_id, SUM(qty * unit_price) AS total
FROM order_items GROUP BY order_id
)
SELECT o.id, o.created_at, t.total
FROM orders o
JOIN totals t ON t.order_id = o.id
WHERE (o.created_at, o.id) < ($1, $2) -- keyset cursor, not OFFSET
ORDER BY o.created_at DESC, o.id DESC
LIMIT 20;
Composite index matching a query
-- WHERE tenant_id = $1 AND status = $2 ORDER BY created_at DESC
CREATE INDEX idx_orders_tenant_status_created
ON orders (tenant_id, status, created_at DESC);
See Also
nodejs-backend-expert — calling the DB safely (pooling, parameterization).
performance-expert — end-to-end latency and profiling.
api-design-expert — cursor pagination contracts backed by SQL.
security-expert — parameterization and least-privilege access.
1---2name: sql-expert3description: Expert SQL & relational databases: queries, schema design, indexing, query-plan optimization, and transactions (Postgres/MySQL/SQLite). Trigger keywords: SQL, database, Postgres, MySQL, SQLite, JOIN, index, EXPLAIN, query plan, slow query, N+1, normalization, transaction, isolation, deadlock, migration. Use for writing/optimizing queries, designing schemas, or fixing slow/locking queries.4---56# SQL & Database Expert78> Think in sets, not loops. The database is smarter than your application loop. When something is slow, read the plan (`EXPLAIN ANALYZE`) before guessing — the answer is almost always a missing index or a bad join.910## When to Use11- Writing, debugging, or optimizing SQL.12- Schema design: tables, relationships, constraints, types, migrations.13- Slow queries, missing/unused indexes, N+1, lock contention.14- Transactions, isolation levels, and concurrency correctness.1516## When NOT to Use17- ORM/app wiring → `nodejs-backend-expert` / language skill.18- Vector/semantic retrieval → `rag-expert`.19- Data-platform / warehouse-scale architecture → `software-architect`.2021## Core Principles2223### 1. Schema design24- Normalize to 3NF by default; denormalize only for a **measured** read win, and then keep it consistent (triggers/jobs).25- Right types: `timestamptz` (not naive timestamps), `numeric`/`decimal` for money (never float), `uuid`/`bigint` keys, native `enum`/`boolean`. Enforce integrity at the DB: `NOT NULL`, `UNIQUE`, `FOREIGN KEY`, `CHECK` — the app is not the only writer.2627### 2. Query like a pro28- Select only needed columns (no `SELECT *` in app queries — it breaks covering indexes and over-fetches).29- Know your JOINs and beware **fan-out**: a one-to-many join multiplies rows and inflates `SUM`/`COUNT`. Pre-aggregate in a subquery/CTE.30- Kill **N+1** patterns: one query with a JOIN or `WHERE id IN (...)` instead of a query per row.31- `EXISTS` over `IN (subquery)` for correlated existence checks; window functions for running totals/ranking instead of self-joins.3233### 3. Indexing34- Index columns in `WHERE`, `JOIN`, and `ORDER BY`. **Composite index order**: equality columns first, then the range/sort column. The index serves a left-to-right prefix.35- A function/expression on the column (`WHERE lower(email)=…`, `WHERE created_at::date=…`) defeats a plain index — index the expression or rewrite as a sargable range.36- Indexes speed reads, slow writes, and use space — add deliberately. Drop unused ones. Use partial/covering indexes for hot queries.3738### 4. Read the plan & transactions39- `EXPLAIN (ANALYZE, BUFFERS)`. Red flags: seq scan on a large table for a selective filter, row-estimate vs actual far off (stale stats — `ANALYZE`), nested-loop over huge sets.40- Keep transactions short; choose isolation deliberately (`READ COMMITTED` default; `SERIALIZABLE` for invariants, with retry on serialization failure). Acquire locks in a consistent order to avoid deadlocks.4142## Common Mistakes43- **`SELECT *`** in application code → over-fetch, fragile, no covering index.44- **Aggregates over a fanned-out JOIN** → doubled sums; aggregate before joining.45- **Functions on indexed columns in `WHERE`** → full scan.46- **Wrong composite-index column order** → index unused for the query.47- **Offset pagination on huge tables** (`OFFSET 100000`) → slow; use keyset/cursor pagination.48- **Floating-point money** → rounding errors; use `numeric`.49- **Implicit type mismatch** in joins/filters → silent full scans.5051## Examples5253**Avoid JOIN fan-out with a CTE; keyset pagination**54```sql55WITH totals AS (56 SELECT order_id, SUM(qty * unit_price) AS total57 FROM order_items GROUP BY order_id58)59SELECT o.id, o.created_at, t.total60FROM orders o61JOIN totals t ON t.order_id = o.id62WHERE (o.created_at, o.id) < ($1, $2) -- keyset cursor, not OFFSET63ORDER BY o.created_at DESC, o.id DESC64LIMIT 20;65```6667**Composite index matching a query**68```sql69-- WHERE tenant_id = $1 AND status = $2 ORDER BY created_at DESC70CREATE INDEX idx_orders_tenant_status_created71 ON orders (tenant_id, status, created_at DESC);72```7374## See Also75- `nodejs-backend-expert` — calling the DB safely (pooling, parameterization).76- `performance-expert` — end-to-end latency and profiling.77- `api-design-expert` — cursor pagination contracts backed by SQL.78- `security-expert` — parameterization and least-privilege access.