From f0d32501e4f810ec485b13d69ed3391a7667ec3c Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 22 Feb 2026 13:16:19 +0100 Subject: [PATCH] location_postcodes does geometry lookups on centroid --- lib-sql/functions/utils.sql | 3 +-- lib-sql/tables/postcodes.sql | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib-sql/functions/utils.sql b/lib-sql/functions/utils.sql index f41bc757..bc2dfdca 100644 --- a/lib-sql/functions/utils.sql +++ b/lib-sql/functions/utils.sql @@ -153,8 +153,7 @@ BEGIN IF ST_GeometryType(geom) in ('ST_Polygon','ST_MultiPolygon') THEN SELECT min(postcode), count(*) FROM (SELECT postcode FROM location_postcodes - WHERE geom && location_postcodes.geometry -- want to use the index - AND ST_Contains(geom, location_postcodes.centroid) + WHERE ST_Contains(geom, location_postcodes.centroid) AND country_code = country LIMIT 2) sub INTO outcode, cnt; diff --git a/lib-sql/tables/postcodes.sql b/lib-sql/tables/postcodes.sql index 9d323f58..2f508aa4 100644 --- a/lib-sql/tables/postcodes.sql +++ b/lib-sql/tables/postcodes.sql @@ -23,6 +23,8 @@ CREATE UNIQUE INDEX idx_location_postcodes_id ON location_postcodes USING BTREE (place_id) {{db.tablespace.search_index}}; CREATE INDEX idx_location_postcodes_geometry ON location_postcodes USING GIST (geometry) {{db.tablespace.search_index}}; +CREATE INDEX idx_location_postcodes_centroid ON location_postcodes + USING GIST (centroid) {{db.tablespace.search_index}}; CREATE INDEX IF NOT EXISTS idx_location_postcodes_postcode ON location_postcodes USING BTREE (postcode, country_code) {{db.tablespace.search_index}}; CREATE INDEX IF NOT EXISTS idx_location_postcodes_osmid ON location_postcodes