Indexes and B-Trees: The Cost on Every Write
Understand the idea
Why an index makes reads 1,000x faster, but exacts a tax on every single insert and update.
The idea
A database is a helper data structure that the database maintains to make searching specific columns much faster. Without an index, the database has to do a : reading every single row off the disk into memory and checking them one by one. If you have 50 rows, that takes less than a millisecond; if you have 5,000,000 rows, it can take many seconds and freeze your database.
The standard index used by relational databases is the (Balanced Tree). A B-tree keeps values sorted in a balanced tree structure. Even if a table holds 100 million rows, a B-tree only needs 3 or 4 quick hops to find any exact row, making lookups nearly instant. B-trees work great for both exact matches (WHERE email = 'alice@example.com') and ranges (WHERE price BETWEEN 10 AND 50).
However, indexes come with real costs:
- Disk Space and Memory: Indexes take up extra storage on disk and use up room in the database server's RAM.
- Slower Writes: Every time you insert, update, or delete a row, the database has to update the table AND rebalance every index on that table. An unindexed table writes quickly; a table with six indexes has to do extra work on every single write.
What changes
| Metric | Unindexed Column | B-Tree Indexed Column |
|---|---|---|
| Lookup Complexity | O(N) full table scan (slower as table grows) | O(log N) logarithmic lookup (sub-millisecond) |
| Range Scan Speed | Linear crawl through all rows | Extremely fast: navigates to start key and scans leaf nodes |
| Write Latency (INSERT) | Minimal: appends row to end of data page | Higher: must insert key and rebalance B-tree structure |
| Storage Consumption | Zero index storage | Can consume 20% to 100% of the table's data size |
Sequential scan versus index
SELECT * FROM records WHERE key = 42;
SCAN · 0 / 8 row checks
INDEX · 0 / 3 steps
20 | 40 | 60
row 6 · key 42
Waiting for lookup
Illustrative work count: one scan row per step; one index page or row fetch per step.
Find every row where key = 42. Compare a full scan with a small B-tree index.
01 / 09Sequential scan versus index
The command
Measuring write amplification across multiple indexes
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see how maintaining multiple indexes multiplies disk I/O operations on bulk write workloads.
Guided challenge
Determine query search complexity
Update the function to return 'O(log N)' when hasIndex is true, and 'O(N)' when false.
Determine query search complexity
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
O(log N) O(N)
Hints 0/1
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.