1. Start at Third Normal Form (3NF), Denormalize Only With Evidence
Premature denormalization—storing comma-separated tags, duplicating mutable user emails across twelve tables, or copying order totals without line-item constraints—creates update anomalies that corrupt production data quietly over months.
Model your core transactional entities in Third Normal Form first: every non-key attribute depends on the key, the whole key, and nothing but the key. Only introduce deliberate denormalization (such as precomputed workspace counters, materialized summary columns updated in the same transaction, or JSONB document attributes for genuinely sparse metadata) after profiling reveals an actual join bottleneck.
2. Always Index Foreign Keys Explicitly (Especially in PostgreSQL & SQL Server)
A surprisingly common performance incident hits teams weeks after launch: MySQL InnoDB automatically creates an index on a foreign key column if none exists, whereas PostgreSQL and SQL Server do NOT automatically index referencing foreign key columns.
Without an index on `order_items(order_id)`, deleting or updating a parent `orders` row forces PostgreSQL to sequentially scan the entire `order_items` table to check referential integrity—holding locks that stall concurrent checkouts.
Audit every FOREIGN KEY in your ERD before deploying: pair each child FK column with a dedicated index (or make it the leading column of a composite index) in PostgreSQL and SQL Server.
3. Master Composite B-Tree Index Order: Equality First, Sort/Range Next
Adding three single-column indexes (`idx_workspace`, `idx_status`, `idx_created_at`) is rarely optimal for multi-tenant SaaS queries like listing active orders for one workspace sorted newest first. A single well-ordered composite B-tree index satisfies both filtering and `ORDER BY` without an in-memory sort.
Use the ESR rule—Equality columns first, Sort columns second, Range columns next—and leverage partial indexes (`WHERE deleted_at IS NULL`) to shrink index RAM footprint by excluding cold soft-deleted rows.
-- Query pattern:
-- SELECT id, customer_id, total_minor, created_at
-- FROM orders
-- WHERE workspace_id = $1 AND status = 'PAID' AND deleted_at IS NULL
-- ORDER BY created_at DESC
-- LIMIT 25;
-- PostgreSQL: Equality (workspace_id, status) -> Sort (created_at DESC) + Partial filter
CREATE INDEX CONCURRENTLY idx_orders_ws_status_recent
ON orders (workspace_id, status, created_at DESC)
INCLUDE (customer_id, total_minor)
WHERE deleted_at IS NULL;
-- MySQL 8+: Composite covering index (no partial WHERE clause syntax)
CREATE INDEX idx_orders_ws_status_created
ON orders (workspace_id, status, created_at DESC, customer_id, total_minor);4. Primary Keys: Time-Ordered UUIDv7 vs. Random UUIDv4 vs. BIGINT
Choosing primary keys affects storage density, B-tree page fragmentation, and external API enumeration risk:
| Key Type | Size | B-Tree Insert Locality | Best Use Case |
|---|---|---|---|
| BIGINT / IDENTITY | 8 bytes | Sequential (optimal append) | High-volume internal fact/ledger tables, append-only logs |
| UUIDv7 (Time-Ordered) | 16 bytes | Monotonic prefix (minimal page splits) | Public tenant/resource IDs needing safe unguessable URLs + B-tree speed |
| UUIDv4 (Random) | 16 bytes | Random scatter (heavy WAL & cache churn) | Legacy integrations where chronological ordering isn't required |
5. Zero-Downtime Migrations via Expand-and-Contract
Running `ALTER TABLE users RENAME COLUMN name TO full_name` during a rolling deployment guarantees HTTP 500 errors because old application pods query `name` while new pods expect `full_name`.
Instead, apply the Expand-and-Contract pattern across three safe deploys: (1) Expand—add the nullable `full_name` column (and `CREATE INDEX CONCURRENTLY` outside transactions in Postgres) while app writes dual-populate both columns and backfills older rows in small batches; (2) Switch—deploy readers exclusively to `full_name`, then validate `NOT NULL` safely using `CHECK ... NOT VALID` + `VALIDATE CONSTRAINT` to avoid long access-exclusive table locks; (3) Contract—drop the deprecated column once traffic drains cleanly.
Turn architecture guides into living diagrams
Start free with 5 boards and 20 AI credits/month—spanning interactive HLD Canvas, LLD UML modeling, Database Schema Studio, and Repository Architecture mapping.