Why nearby is a string problem

z = interleave(lat, lon)

Interleaving latitude and longitude bits into one sortable string turns proximity into a prefix, but a radius is a disc, so the index only nominates candidates.

Proximity becomes a prefix once the two coordinates are folded into one string. Latitude and longitude bits alternate, and each bit halves the range it refines, so every point inherits the code of the smallest cell that contains it. Points in the same cell share a leading substring, which keeps them adjacent in a sorted index. A single B-tree range scan can therefore fetch a whole box, but no single prefix can describe a circle.

Take a query for points within a radius of a searcher. Compute the searcher's geohash at a chosen precision, which fixes the cell size, then list the cells the circle's bounding box overlaps. Each cell is a prefix, and each prefix maps to a lexicographic interval running from that prefix to the next one in sort order. The database runs two binary searches per interval, a lower bound and an upper bound, then reads the rows between them, already stored in geohash order. For a radius about one cell across, the query touches the searcher's cell plus eight neighbours, so the work is nine interval lookups rather than one. Each lookup costs log n comparisons, and the rows returned are cells, not circles, so every candidate still needs an exact distance check.

The abstraction hides the boundary. Two very close points on opposite sides of a cell edge can share no prefix at all, so a lookup that reads only the searcher's own cell never sees the other one. Take a store finder that searches just that cell. A user standing near a boundary sees the shops on their side and misses the ones a few steps across the line. Nothing errors and nothing times out; the map quietly returns a share of the shops within the radius. The cause is that a prefix preserves containment, not distance. The index is correct about boxes, and the bug is treating a box as a circle.

Choose the cell precision from the radius you must support, aiming for roughly one cell across it. Enumerate every cell the circle intersects, neighbours included, and run one range scan per cell. Deduplicate the candidates, since a point near a corner can return from more than one scan. Filter by exact distance and discard the false positives. If the data is sharded, group the cells by shard before issuing scans, and price each scan as a network trip. Do not reach for this structure unless the query really is nearby-me. A rectangular range is served well by a two-column index, and a geohash adds boundary code for no gain.

orders of magnitude
cells touched per query
9

The searcher's cell plus eight neighbours, the minimum a disc query covers at a one-cell radius.

range scans per cell
1

Each cell prefix becomes one lexicographic interval, read with two binary searches.

precision per character
1

Each added character refines the cell further, so string length sets cell size.

rows fetched beyond the answer
more

Cells are boxes, so candidates outside the circle come back and the exact distance filter removes them.

field note

Uber's H3 is a hierarchical hexagonal grid built for indexing locations. The naive design indexes latitude and longitude as two separate columns. A nearby query then becomes two range scans, one per axis, and the database must merge two result sets that arrive in no useful order. The intersection describes a rectangle wider than the circle people care about.

The wall is the merge. Reading both indexes and intersecting them costs two scans plus a sort the query never needed, and the result is still not ordered by distance. H3 instead assigns each location one cell identifier at a chosen resolution. Resolution is the dial: a coarse level covers a wide area and a fine level covers a small one, so precision is traded directly for area.

The non-obvious lesson is that the grid does not remove the boundary. A query must still include the cells around the one it lands in, then filter by exact distance. Dimensionality is traded for ordering, and the neighbour logic is code the application owns. Treat the cell as a candidate generator, not as the answer.