Part 11 of 12 · Data Structures

How do you find what's nearby?

Among a million points across India, finding everything within 1 km took 104 ms with a full scan, 1.2 ms with geohash cells and 43 µs with a quadtree, all returning the same results. Geohashes are simple and fit any key-value store; quadtrees adapt to density. Measured, plus what PostGIS and Redis…

A brass compass resting on an old map

The verdict, which the measurements below will prove: never compare against every point. Index space so a query only looks at points that could be close, then check exact distances on those few.

  • Geohash: turn a location into a short string where nearby places share a prefix. Store points by cell; a query reads its own cell and the 8 around it. Works in any database or key-value store.
  • Quadtree (or an R-tree, its cousin used by databases): split space into boxes that get smaller where points are dense. A query descends only into boxes that overlap the search area.
  • In production: use PostGIS (ST_DWithin with a GiST index), Redis (GEOSEARCH, which is geohash-based), or your search engine’s geo queries.

Here’s the case: a million points, a user asking “what’s near me?”, and a query that takes a tenth of a second when it should feel instant. The first suspect is the maths: surely it needs serious geometry. But the best answers are simple ideas about cutting space into pieces, and the real culprit turns out to be looking at points that could never matter. The numbers below show how much those ideas buy you.

The code is in systems-data-structures (Go, standard library only).

Case file

The case “What’s near me?” takes a tenth of a second over a million points
Suspects The maths: serious geometry
Culprit Looking at points that could never matter
The fix Index space: geohash cells or a quadtree, then exact distances
Evidence 1,000,000 points across India, Go 1.27 on a laptop, code in systems-data-structures

The first suspect: geohash #

Gustavo Niemeyer’s geohash (2008) is one of those ideas that makes you smile the first time you see it. Split the world in two by longitude: left half 0, right half 1. Split that half by latitude, and so on, alternating. After 25 bits, the cell is about 5 km across; written in base 32 that’s five characters, like tdr1v. Nearby points usually share a prefix, so “points in cell tdr1v” is a prefix lookup. Two dimensions, folded into one string you can sort.

The catch, and it’s a sneaky one: two points can be a metre apart across a cell boundary and share no prefix. So a query always reads the 3 × 3 block of cells around the location, at a precision where a cell is at least as large as the radius.

Flowchart: User location, Geohash cell at a precision ≥ radius, Read the 3 × 3 block of cells, Candidate points, Exact distance ≤ radius?, Result, DropFlowchart: User location, Geohash cell at a precision ≥ radius, Read the 3 × 3 block of cells, Candidate points, Exact distance ≤ radius?, Result, Drop
Diagram source
flowchart LR
  L["User location"] --> C["Geohash cell at a precision ≥ radius"]
  C --> N["Read the 3 × 3 block of cells"]
  N --> P["Candidate points"]
  P --> D{"Exact distance ≤ radius?"}
  D -- yes --> R["Result"]
  D -- no --> X["Drop"]

The second suspect: the quadtree #

Start with one box for the whole map. When a box holds more than 32 points, split it into four. City centres end up with tiny boxes; the countryside keeps big ones. A query walks down from the top, skipping every box that doesn’t overlap the search area. The structure grows its own detail wherever the data is busy, which is the elegant part: nobody has to tell it where the cities are.

The evidence: a million points #

Theories are cheap. Let’s put all three suspects in the same room and see what actually happens with real numbers.

1,000,000 points across India, half clustered around ten city centres, half spread out. 200 queries per radius, half in a city centre and half anywhere. All three methods return the same points (the tests check this exactly):

Radius Method Points checked per query Found (avg) Time per query
1 km full scan 1,000,000 165* 104 ms
1 km geohash cells 8,243 160 1.2 ms
1 km quadtree 204 160 43 µs
2 km full scan 1,000,000 632* 107 ms
2 km geohash cells 8,285 639 1.4 ms
2 km quadtree 809 639 161 µs
5 km full scan 1,000,000 3,707* 131 ms
5 km geohash cells 29,536 3,733 4.0 ms
5 km quadtree 4,635 3,733 851 µs

*The full scan ran only the first 20 of the 200 queries, so its average is over a different sample.

Chart: Time per query in ms: full scan vs geohash (quadtree: 43 µs to 851 µs)Chart: Time per query in ms: full scan vs geohash (quadtree: 43 µs to 851 µs)
Diagram source
xychart-beta
  title "Time per query in ms: full scan vs geohash (quadtree: 43 µs to 851 µs)"
  x-axis ["1 km", "2 km", "5 km"]
  y-axis "ms per query"
  bar [104 , 107 , 131]
  bar [1.2 , 1.4 , 4.0]

Build time: geohash 1.4 s (six precision levels), quadtree 0.3 s.

The puzzling clue, why geohash checked so many more points: cell sizes jump by a factor of 4-8 between precisions, so for a 1 km query the smallest cell that’s big enough is about 5 km × 5 km, and 9 of those in a dense city hold thousands of points. A quadtree’s boxes follow the data, so it checks only a little more than the answer: 204 points checked to find 160, which is about as close to perfect as an index gets. Indexing geohashes at every 2 bits instead of every 5 would narrow the gap.

Where each one fits #

With the evidence in, the right tool depends on the shape of the question:

Question What I’d use
Nearest stores or warehouses to a customer (thousands of locations) PostGIS ST_DWithin + ORDER BY location <-> point LIMIT 10 with a GiST index; or compute distances to all of them, since thousands is nothing
Drivers near a pickup, locations updating every few seconds Redis GEOADD / GEOSEARCH ... BYRADIUS (geohash-based, in memory), or geohash cells as keys in any store
Is this address inside a delivery zone? Polygons in PostGIS with ST_Contains; precompute zone per geohash cell for very hot paths
Map tiles and clustering markers A quadtree or tile-based grid (map tiles are a quadtree)

Traps for the unwary #

One last twist in the case: the Earth is not a flat grid.

  • Distances on a sphere: use haversine (or the database’s geography type) for anything beyond a few kilometres; degrees of longitude shrink towards the poles.
  • The date line and the poles break naive bounding boxes; libraries handle them, hand-written code often doesn’t.
  • Moving objects: rebuilding a tree for every location update is wasteful; for fast-moving fleets, geohash cells (delete from old cell, add to new) are simpler.

Remember this

  • Never compare against every point: index space first.
  • Geohash reads its own cell and the 8 around it, in any key-value store.
  • Quadtree boxes follow the data: 204 points checked to find 160 at 1 km

Case closed: what I’d do by default #

  1. PostGIS when the data is in PostgreSQL; Redis geo for fast-moving locations.
  2. Geohash cells as keys when you need geo search on a plain key-value store.
  3. Quadtrees or R-trees for dense, uneven data and map rendering.
  4. Always filter by exact distance after the index narrows the candidates.

Going from 104 ms to 43 µs by drawing boxes differently still delights me. Nothing got faster; we just stopped looking. That’s how much speed hides in not looking at what can’t matter.

Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U). Points checked and results come from seeded data and repeat exactly; times depend on the machine. Code and full results: systems-data-structures.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.