R-tree (spatial index)
A tree that groups nearby shapes into nested bounding boxes, so maps can quickly answer “what is near here” and “what is inside this area”.
A B-tree sorts along one axis, but a map has two. An R-tree groups nearby points and shapes into bounding rectangles, groups those rectangles into bigger ones, and so on up to the root. A query for a map area or the nearest store descends only into rectangles that overlap the area and skips the rest. Antonin Guttman introduced it in 1984.
In Postgres, PostGIS builds spatial indexes as R-trees on top of GiST; SQLite has an R-tree module, and most map and GIS software uses something similar. The same idea also indexes time ranges and other intervals that can overlap.
Alternatives turn two dimensions into one: geohash and similar grid codes give each area a string, so an ordinary index on the prefix finds nearby cells. They are simpler and work in any database, but neighbors across a cell border need extra care.
Sources
- ACM SIGMOD ’84: Guttman, R-Trees: A Dynamic Index Structure for Spatial Searching doi.org
- PostGIS workshop: Spatial Indexing postgis.net
Related terms
- Database index: An extra data structure a database keeps next to a table so it can find matching rows without reading the whole table.
- B-tree: A sorted, wide and shallow tree that stores keys in disk-sized pages; the default index in most relational databases.
- HNSW (hierarchical navigable small world): A layered graph index that finds approximately nearest vectors fast; the default index in most vector databases, trading a little recall for speed.
