Database Cheat Sheet

“Problem says X → think Y.” One line per need; click a row to open the lesson.

Indexing

Modeling

SQL

Performance

Transactions

Scaling

Caching

NoSQL

Internals

Why does the engine read 8 KB to fetch one 100-byte row?->The page is the unit of I/O and cachingA record grew on UPDATE and the row "moved"->Slotted page: slots stay, offsets change; overflow to another pageHow does an index find a row in 3 reads among millions?->B+ tree: fanout ~200, height 3–4, linked leavesWhy not a balanced binary tree for the index?->Fanout: 24 random page reads vs 3Index on = only, never ranges->Hash index: bucket lookup, no orderingSecond run of the query is 100× faster->Buffer pool hit vs missA big scan evicted the working set->Replacement policy: Clock, ring buffers, old/young listsWhat does COMMIT wait for?->WAL fsync — not the data pagesCrash after COMMIT — is the data safe?->Yes: recovery replays the WAL from the last checkpointTwo transactions want the same row->Lock manager: S/X compatibility, wait queueDeadlock detected, transaction aborted->Cycle in the waits-for graph; one victim chosenReaders never block writers — how?->MVCC: version chains + snapshot visibility ruleTable keeps growing after DELETE->Dead tuples until VACUUM; long snapshots pin themSerialization failure, please retry->SSI detected a rw-dependency cycleMillions of writes per second, mostly inserts->LSM tree: WAL → memtable → SSTable → compactionPoint read checks many files->Bloom filter per SSTable skips the definite missesDisk usage 3× the data on an LSM store->Space amplification; compaction backlogB+ tree or LSM for this workload?->Reads and ranges → B+; write-heavy ingest → LSMPlanner picked Seq Scan although an index exists->Cost model: selectivity × random page cost > sequentialWhich join algorithm will it use?->Nested loop (small/indexed), hash (equality, fits memory), merge (sorted inputs)Why is the InnoDB secondary lookup two descents?->Secondary index → PK → clustered indexReplica shows old data->Async replication lag: LSN on replica < primaryAdding a node moved 80% of keys->Consistent hashing / virtual nodes instead of moduloPrimary died — who is leader now?->Election with a majority quorum; fence the old primarySlow, and EXPLAIN is not enough->Count pages, buffer misses, sorts, WAL, contention