<< All versions

Skill v1.0.0

currentAutomated scan100/100
kendrick-oppong/geoskills/spatial-indexing-strategy
──Details
PublishedOctober 25, 2026 at 06:24 AM
Content Hashsha256:8f671d0268798fe6...
Git SHA77bd91343bcb
──Files
Files (1 file, 4.6 KB)
SKILL.md4.6 KBactive
SKILL.md · 62 lines · 4.6 KB

version: "1.0.0" name: spatial-indexing-strategy description: Use when deciding how to index or partition spatial data for query performance — R-tree vs quadtree vs geohash vs H3 vs S2, database spatial indexes, and when to denormalize location into a discrete grid cell for sharding, caching keys, or aggregation.


Spatial Indexing Strategy

When to use this skill

  • "This spatial query is slow" (before reaching for hardware)
  • Choosing a grid system for aggregation, caching keys, sharding, or "nearby" lookups
  • Designing a schema that will hold millions+ of geometries

Decision framework

MechanismBest forNotes
R-tree (PostGIS GiST, SQLite R*Tree)General-purpose bounding-box index on arbitrary geometry (points, lines, polygons)Default choice inside a relational database; use for ST_Intersects, ST_DWithin, bbox filters
QuadtreeHierarchical spatial subdivision, tile pyramids, client-side spatial trees (e.g. in-browser clustering)Natural fit for tile-based web maps; used internally by many tiling tools
GeohashString-prefix-based proximity, sortable spatial keys in a plain key-value store (Redis, DynamoDB)Cheap to implement; has "edge" discontinuity artifacts near boundaries — two nearby points can have very different prefixes
H3 (Uber)Hexagonal global grid for aggregation, binning, and analytics (e.g. "trips per hex per hour")Hexagons have uniform neighbor distance/adjacency, unlike geohash's rectangular cells — better for spatial statistics and heatmaps
S2 (Google)Global cell hierarchy for very large-scale, precision-sensitive indexing (e.g. global coverage systems)More complex API, strong for polygon coverage approximation at web scale
Grid-based sharding keyPartitioning a huge table/cluster by regionPick cell size so shards are balanced by data density, not raw area — dense urban cells need finer partitioning than sparse rural ones

Checklist

  • [ ] Is there a spatial index on the geometry column at all? (CREATE INDEX ... USING GIST in PostGIS) — the single highest-leverage fix for slow spatial queries
  • [ ] Are you comparing geometry (geometry type, planar) or geography (geography type, ellipsoidal) — geography indexes/queries are more expensive; use geometry + a projected CRS if raw speed matters and the area is regional, not global
  • [ ] Is the query pattern "nearest neighbor"? Use a KNN-aware index/operator (PostGIS <->), not a brute-force ORDER BY ST_Distance
  • [ ] Is the query pattern "aggregate by area" (heatmaps, choropleths, density)? Precompute an H3/grid join rather than doing live polygon aggregation per request
  • [ ] For a cache or NoSQL key design, does the chosen grid resolution match the query radius? Too coarse = large fan-out per lookup; too fine = many cells to union for a wide search radius
  • [ ] Are indexes actually being used? Check the query plan (EXPLAIN ANALYZE) — a common miss is wrapping the indexed column in a function, which defeats the index unless a matching functional index exists

Common pitfalls

  • Missing GiST index entirely: the most common root cause of "PostGIS is slow" — always check first.
  • Geography type used for regional-only data: geography computes on a spheroid and is noticeably slower than geometry + local projection for area-bounded apps (e.g. a single-country dashboard).
  • KNN via `ORDER BY ST_Distance(...) LIMIT n` without the `<->` operator: forces a full table scan instead of an index-assisted nearest-neighbor search.
  • Geohash boundary discontinuity: two points a meter apart on opposite sides of a geohash cell boundary can share zero prefix characters — don't use raw geohash proximity for "find nearby" without also querying neighbor cells.
  • Over-fine H3 resolution for the data density: aggregating sparse rural data at a fine hex resolution produces mostly-empty cells and wastes storage/compute — resolution should match feature density, ideally chosen per-region.

Reference snippets

sql
CREATE INDEX idx_places_geom ON places USING GIST (geom);
-- KNN: nearest 10 places to a point, index-assisted
SELECT id FROM places ORDER BY geom <-> ST_SetSRID(ST_MakePoint(:lon, :lat), 4326) LIMIT 10;
python
import h3
cell = h3.latlng_to_cell(lat, lon, 9) # resolution 9 hex, ~0.1 km2 — pick per use case

Related skills

  • postgis-geospatial — indexing inside PostgreSQL specifically
  • duckdb-spatial-geoparquet — indexing/partitioning strategy for analytical/columnar workloads
  • webgis-performance-optimization — client-side spatial data structures (quadtrees for clustering, etc.)
All versions