Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

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:

  1. Disk Space and Memory: Indexes take up extra storage on disk and use up room in the database server's RAM.
  2. 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

Unindexed Table vs B-Tree Indexed Table
MetricUnindexed ColumnB-Tree Indexed Column
Lookup ComplexityO(N) full table scan (slower as table grows)O(log N) logarithmic lookup (sub-millisecond)
Range Scan SpeedLinear crawl through all rowsExtremely fast: navigates to start key and scans leaf nodes
Write Latency (INSERT)Minimal: appends row to end of data pageHigher: must insert key and rebalance B-tree structure
Storage ConsumptionZero index storageCan 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

row 1 · key 10waiting
row 2 · key 25waiting
row 3 · key 15waiting
row 4 · key 60waiting
row 5 · key 35waiting
row 6 · key 42waiting
row 7 · key 5waiting
row 8 · key 70waiting

INDEX · 0 / 3 steps

Root page
20 | 40 | 60
Leaf page · key 42 → row 6
↓ Direct row fetch
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.

    01Why does creating an index slow down database write operations (INSERT, UPDATE, DELETE)?
    02What data structure serves as the standard default indexing mechanism in relational databases?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson