So I have this simple query, selecting all points in my table that are within a given polygon:
WITH my_polygon as (
SELECT ST_GeomFromEWKT('<EWKT polygon>') as geom
)
SELECT count(*)
FROM points as a
, my_polygon as p
WHERE ST_Within(points.geom, my_polygon.geom);
I have two equal points tables, both consisting of the same kind of points, scattered globally. The only difference is:
- One is sparse _____ 500.000 rows
- One is dense __ 340.000.000 rows
I have now executed the query on both tables, testing with and without gist-index on the points.geom column. On the sparse table I get these execution times:
- No indexing ________ 483.544 ms
- With gist index ________ 0.142 ms
Looks good, index is working properly. Now for the dense table:
- No indexing _____ 195307.138 ms
- With gist index ___ 234495.684 ms
This was not as expected. Is the gist index not supposed to speed up this query? The explain analyze for all cases is provided in this GitHub-Gist
(I did performed vacuum analyze after creating the index. Also I see the same behavior when gin-indexing an array. Query slows down on the big table using the index.)