Key Terms
| Term | In one line |
|---|---|
| B-tree | Balanced sorted tree — the default index; supports range and prefix queries |
| Hash index | Equality only, no ranges |
| GIN | Inverted index for arrays, JSONB, full-text |
| BRIN | Min/max per block range — tiny index for naturally ordered huge tables |
| Covering index | Includes the selected columns so the engine never does a key lookup |
| Index seek vs scan | Traversal straight to matching rows vs reading the whole index |
| Key lookup | Going back to the clustered index for columns the index didn’t cover |
| Sargable | A predicate the optimizer can use an index for — WHERE f(col) = x is not |
| RCSI | Read Committed Snapshot — readers don’t block writers |
| Materialized view | Precomputed, stored query result; must be refreshed |
| Cache-aside | App checks cache, on miss loads from DB and populates — the default pattern |
Numbers That Matter
| Isolation level | Dirty read | Non-repeatable read | Phantom |
|---|---|---|---|
| Read uncommitted | Yes | Yes | Yes |
| Read committed (most defaults) | No | Yes | Yes |
| Repeatable read | No | No | Yes |
| Serializable | No | No | No |
| Figure | Value |
|---|---|
| Normal forms | 1NF atomic · 2NF no partial dependency · 3NF no transitive dependency · BCNF every determinant is a candidate key |
| Cache layers | CDN → gateway response cache → in-process L1 (~10K hot keys) → distributed L2 (Redis) → DB |
| Sharding strategies | Hash · range · directory · tenant |
Decision Rules
- Diagnose a slow table before touching it: read the execution plan first. Table scan where you expected a seek, a key lookup on a hot query, an implicit conversion killing sargability, or stale statistics. Add the index the plan asks for, not the one you guessed.
- Non-sargable predicates are the most common self-inflicted wound: wrapping the indexed column in a function, leading wildcards, or an implicit type conversion silently disables the index.
- Denormalize deliberately, for a measured read pattern — and own the write-time cost of keeping copies consistent. Normalize by default; 3NF is the right starting point.
- Choose SQL when relationships and joins are central, ACID matters, or reporting is complex. Choose NoSQL when the schema evolves, write throughput is extreme, the data is document-shaped, or you need multi-region active-active.
- Match the NoSQL type to the access pattern: document for hierarchical aggregates, key-value for cache/session, wide-column for massive time-ordered writes, graph for traversals, time-series for metrics, search for full-text and facets.
- Read replicas solve read scale, not write scale, and they introduce replication lag — so route reads that must be fresh back to the primary, or use session consistency.
- Connection pooling is a scaling limit people forget. Every serverless instance opening its own pool will exhaust the database long before CPU does — put a proxy or pooler in front.
- Cache-aside as default, write-through when reads must never be stale, write-behind only when you can tolerate loss on failure, refresh-ahead for predictable hot keys.
Say This Out Loud
“A hundred million rows and slow queries — I’d start with the execution plan rather than adding indexes. Usually it’s one of four things: a table scan where I expected a seek, a key lookup on a hot path that a covering index would remove, a non-sargable predicate where someone wrapped the indexed column in a function, or statistics so stale the optimizer is choosing badly. Then I’d look at whether the working set still fits in memory, because that’s the cliff where everything degrades at once.”
“For a million-product catalog I’d layer the cache rather than reach for one. CDN for images and static assets, response caching at the gateway for idempotent GETs, a small in-process L1 holding the ten thousand hottest products to avoid a network hop, and Redis as the shared L2. The interesting design question isn’t the layers, it’s invalidation — I’d drive it off product-update events rather than short TTLs, because TTL-based invalidation means you’re either serving stale prices or hammering the database.”
“I’d default to third normal form and denormalize only where I’ve measured a read pattern that needs it. Denormalization isn’t free — every duplicated field is a consistency obligation at write time, and the bug that follows is always the copy nobody remembered to update.”