← Course home

Advanced applications · Lesson 51 of 52

Storage engines and locality

Compare B-tree and LSM costs across reads, writes and locality.

Full lessonAbout 10 min

You will learn to compare B-tree and LSM work placement and explain effects of locality and storage reads.

Recommended first: Balanced search trees, Range-query structures.

On this page 6 sections

The idea

sequential_scan_sum of 1, 2, 3 returns 6.

Storage engines place reads and writes in different structures. A B-tree keeps keys ordered in nodes with many children, so a lookup walks a short path. BTree.insert of 2 and then 1 leaves search(1) true and search(9) false. The branching factor is the knob that trades taller trees for fatter nodes. A log-structured merge tree takes the other side of the trade. Writes append to a memory table and a write-ahead log, then flush sorted runs. Reads may check several runs. Compaction merges those runs. The library's sequential_scan_sum of 1, 2, 3 returns 6. That scan touches neighboring items, which is the access pattern real disks and caches reward. A random jump to each key pays the same arithmetic and a worse memory pattern. Locality is why the sum of a tight array is the baseline you compare against.

A worked trace

StepStateWhat changed
Scansum = 1sequential_scan_sum of [1] returns 1.
Scansum = 3sequential_scan_sum of [1, 2] returns 3.
Scansum = 6sequential_scan_sum of [1, 2, 3] returns 6.

Why it is correct

B-tree search is correct because each node stores its keys in order. The child between two keys holds only the keys that belong between them. A missing key falls out of a leaf. Insert splits a full node so the branching invariant survives. A write-ahead log is correct when a replay reproduces the same committed writes. A sequential scan is correct because addition is applied once per item and the items are exactly the input. The locality claim is not a change in the arithmetic result. It is a claim about how many cache lines or pages that arithmetic touches.

A common mistake

The same arithmetic on a jumping scan still returns 6. Locality changes the practical cost.

What it costs

A B-tree search is logarithmic in the number of keys, with a large base when nodes are wide. Extra memory is the nodes. A sequential scan is linear time and constant extra memory, and it is the practical winner when you need every item anyway. A point lookup in a structure built for scans can be slower than the same lookup in a tree. Say which operation is common before you pick the engine. The toy sum is the locality baseline, not a database. A sequential scan is Θ(n) time and O(1) extra memory.

Run it

python
from bitsandbytes.storage.engines import sequential_scan_sumprint(sequential_scan_sum([1, 2, 3]))# prints: 6 

Practice and next step

Open Approximation guarantees next. Approximation guarantees bound how far a legal answer can be from the best answer.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.storage.engines

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

A B-tree lookup and a sequential scan

BTree.search finds 1 after it was inserted and rejects 9. sequential_scan_sum of 1, 2, 3 returns 6.

sum
1

State

  1. 01done

sequential_scan_sum of [1] returns 1.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1A B-tree and a sequential scan solve different workloads. Which claim matches the scan?
2Why can two linear scans with the same arithmetic still differ in practice?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.