I've got two tables conatining geometry: 1 containing big polygon's (regions, srid=4326) and another containing smaller polygon's (pand, srid=28992, containg ~9.9milion rows)
By using one of the big polygons, I want to find all the smaller polygons withing the bigger polygon. In the end I want the result set to be grouped by a column. But now I just want to count the number of rows (=number of objects within the big region)
This is the query I use at the moment:
WITH regio AS ( SELECT ST_Transform(the_geom,28992) as the_geom
FROM public.regions WHERE gid = 622
)
SELECT
count(*) AS count
FROM public.smaller AS p
WHERE
ST_Within ( p.geovlak,
(SELECT the_geom FROM regio)
)
But somehow this query takes ages to complete. This is the Queryplan:
Aggregate (cost=3018553.41..3018553.42 rows=1 width=0)
CTE regio
-> Index Scan using regions_prim_key_gid2 on regions (cost=0.28..8.29 rows=1 width=1458)
Index Cond: (gid = 622)
InitPlan 2 (returns $1)
-> CTE Scan on regio (cost=0.00..0.02 rows=1 width=32)
-> Seq Scan on smaller p (cost=0.00..3010265.32 rows=3311910 width=0)
Filter: st_within(geovlak, $1)
I'm no expert in reading these queryplans; but the second to last row says there are ~3.3 rows instead of the ~9.9. So there is already some filtering in place. But
1 - what filtering is this? And
2 - I can't figure out where the problem in this query lies.
When changing the count(*) to a * and limit the result to 10; the query takes 99seconds to run, so that's also not fast enough. I can understand it takes some time, but it is still running :(
Spatial indexes:
CREATE INDEX smaller_geom_idx
ON smaller
USING gist
(geovlak);