Here's my query
SELECT "building"."id",
"building"."details",
"building"."status",
ST_AsGeoJSON("building"."center")::json,
ST_AsGeoJSON("building"."geometry")::json
FROM "building" WHERE ST_Intersects(
"building"."center",
ST_MakeEnvelope(-157.91061401367188,21.197376124303936,-157.72951126098633,21.340868251190965,4326):: geography('POLYGON')
)
There are around 500,000 records in my database. These buildings' data are from OSM. I am trying to get the buildings that are inside a bounding box. It takes ~ 50sec to complete the query. My data will grow over time. It might take more time if data grows.
I have indexed my center
column
CREATE INDEX "IDX_32ed92783d0144c88d8094b0ac" ON "building" USING GIST ( center );
My EXPLAIN and ANALYZE:
"Index Scan using ""IDX_32ed92783d0144c88d8094b0ac"" on building (cost=0.28..35.80 rows=1 width=175) (actual time=6.473..146.382 rows=9792 loops=1)"
" Index Cond: (center && '0103000020E61000000100000005000000000000C023BD63C08AE5DE3D87323540000000C023BD63C0231C4724435735400000002858B763C0231C4724435735400000002858B763C08AE5DE3D87323540000000C023BD63C08AE5DE3D87323540'::geography(Polygon,4326))"
" Filter: st_intersects(center, '0103000020E61000000100000005000000000000C023BD63C08AE5DE3D87323540000000C023BD63C0231C4724435735400000002858B763C0231C4724435735400000002858B763C08AE5DE3D87323540000000C023BD63C08AE5DE3D87323540'::geography(Polygon,4326))"
" Rows Removed by Filter: 105"
" Buffers: shared hit=1313"
"Planning Time: 21.729 ms"
"Execution Time: 147.195 ms"
How can I improve my query performance and reduce response time?
Envelope
geometry in other table in order to intersect 2 tables ? Take a look at the functionST_DWithin
to optimize your intersect filter : postgis.net/docs/ST_DWithin.html