Hilbert curves and key locality 00:00:01 A square map becomes a line of keys Four records sit in a square on the floor. A disk file wants a line. Draw a U through this two-by-two grid, visiting each square exactly once by crossing a shared edge. Number them from zero. Start at bottom left with zero, move up to one, right to two and down to three. This particular orientation gives every cell a distinct sortable key. Between zero and one, one and two, or two and three, the scanner crosses one tile edge. The trick is to retain that continuity when a much larger map needs finer keys. Imagine writing the numbers on four floor tiles, then storing the records in increasing number order. A scanner can walk through nearby tiles without jumping across the map at each new record. 00:00:49 Refine while keeping a continuous path Split each of those four cells into four children. In the four-by-four grid there are now sixteen cells, each with one quarter of its former parent’s area. To keep the path continuous, I rotate or flip each small block so the end of one connects smoothly to the start of the next. Order the sixteen keys from zero through fifteen. Cells consecutive along the path share an edge. This spatial locality makes batches of nearby records practical to store and scan. You can check that every small square gets exactly one key and every consecutive pair shares an edge. Those are constructive properties of this finite curve, independent of the colors on a particular diagram. 00:01:31 Find the missing implication Go back to the two-by-two square. Before looking at the keys, ask which two bottom tiles touch. Do those two cells also have consecutive numbers? Bottom left is zero and bottom right is three. They share an entire edge, but their key gap is three. The middle two keys occupy the top row. That observation breaks the reverse implication. Closeness along this path has spatial meaning, yet searching only a few neighboring keys can miss a point across a geographic boundary. Try the same question with the four-by-four refinement. Following consecutive integers stays local, but geographic neighbors can live on different folds and their keys can be widely separated in a larger grid. 00:02:12 Cover a query with intervals Use the bottom row as a query rectangle. Its exact key cover in our two-by-two grid consists of two singleton intervals, one for zero and one for three. Read those intervals, then check actual point positions against the rectangle where a cell is only partly covered. The ordering retrieves candidates and geometry confirms inclusion. A single interval from zero through three also covers the whole bottom row, but reads the unrelated top row. That is two extra cells in this tiny example. If the ranges are expensive to seek, intentionally merge two nearby ranges and filter the gap. The answer stays exact if you accept the extra candidates and still perform the geometry test. 00:02:55 Select detail with a budget With finer precision the boundary cells become smaller. This may reduce the extra area read but split a query into more key intervals. Neither effect alone determines query cost. Now I compare the number of seeks, total cells scanned, and how much filtering is needed to pick the right ones. For nearest-neighbor searches, actual distance matters more than key proximity, so I must use real geometry to confirm results. Keep a reference scan for small maps. A change in orientation can be valid, but writer and reader must agree on the same convention, finite grid, and coordinate-to-cell mapping.