Postgres versions
- PostgreSQL 13.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.2.1_pre1) 10.2.1 20201203, 64-bit
- PostgreSQL 11.11 (Debian 11.11-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
Query
SELECT count(*) FROM "addresses" AS a1 WHERE ST_DWithin('0101000020E61000001EE1B4E0455F0340E92807B309664840'::geography, a1.geom, '25000');
result is as expected result. But slow on PG11 (1.1s) and even slower on PG13 (2.9s)
Table definition
Table "public.addresses"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-------------+--------------------------------+-----------+----------+---------------------------------------+----------+--------------+-------------
id | bigint | | not null | nextval('addresses_id_seq'::regclass) | plain | |
description | text | | | | extended | |
country | character varying(255) | | | | extended | |
locality | character varying(255) | | | | extended | |
region | character varying(255) | | | | extended | |
postal_code | character varying(255) | | | | extended | |
street | text | | | | extended | |
geom | geometry | | | | main | |
inserted_at | timestamp(0) without time zone | | not null | | plain | |
updated_at | timestamp(0) without time zone | | not null | | plain | |
url | character varying(255) | | not null | | extended | |
origin_id | character varying(255) | | | | extended | |
type | character varying(255) | | | | extended | |
Indexes:
"addresses_pkey" PRIMARY KEY, btree (id)
"addresses_origin_id_index" UNIQUE, btree (origin_id)
"addresses_url_index" UNIQUE, btree (url)
"idx_addresses_geom" gist (geom)
Referenced by:
TABLE "actors" CONSTRAINT "actors_physical_address_id_fkey" FOREIGN KEY (physical_address_id) REFERENCES addresses(id)
TABLE "events" CONSTRAINT "events_physical_address_id_fkey" FOREIGN KEY (physical_address_id) REFERENCES addresses(id)
Access method: heap
Cardinalities
SELECT ST_SRID(geom) AS srid, count(*) from addresses group by 1;
srid | count
------+--------
| 1
4326 | 265011
Query plans
PG11
Finalize Aggregate (cost=52276.51..52276.52 rows=1 width=8) (actual time=1082.209..1095.487 rows=1 loops=1)
Buffers: shared hit=3444 read=4611
-> Gather (cost=52276.29..52276.50 rows=2 width=8) (actual time=1082.180..1095.468 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=3444 read=4611
-> Partial Aggregate (cost=51276.29..51276.30 rows=1 width=8) (actual time=897.135..897.138 rows=1 loops=3)
Buffers: shared hit=3444 read=4611
-> Parallel Seq Scan on addresses a1 (cost=0.00..51272.61 rows=1472 width=0) (actual time=6.796..894.827 rows=1960 loops=3)
Filter: (((geom)::geography && '0101000020E61000001EE1B4E0455F0340E92807B309664840'::geography) AND ('0101000020E61000001EE1B4E0455F0340E92807B309664840'::geography && _st_expand((geom)::geography, '25000'::double precision)) AND _st_dwithin('0101000020E61000001EE1B4E0455F0340E92807B309664840'::geography, (geom)::geography, '25000'::double precision, true))
Rows Removed by Filter: 86378
Buffers: shared hit=3444 read=4611
Planning Time: 1.167 ms
Execution Time: 1095.650 ms
PG13 (jit = off)
Finalize Aggregate (cost=2770301.18..2770301.19 rows=1 width=8) (actual time=2801.774..2830.421 rows=1 loops=1)
Buffers: shared hit=8227
-> Gather (cost=2770300.97..2770301.18 rows=2 width=8) (actual time=2800.661..2830.384 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=8227
-> Partial Aggregate (cost=2769300.97..2769300.98 rows=1 width=8) (actual time=2639.846..2639.849 rows=1 loops=3)
Buffers: shared hit=8227
-> Parallel Seq Scan on addresses a1 (cost=0.00..2769300.94 rows=11 width=0) (actual time=63.094..2637.528 rows=1960 loops=3)
Filter: st_dwithin('0101000020E61000001EE1B4E0455F0340E92807B309664840'::geography, (geom)::geography, '25000'::double precision, true)
Rows Removed by Filter: 86378
Buffers: shared hit=8227
Planning Time: 0.424 ms
Execution Time: 2830.699 ms
There seems to be two issues : 1/ spacial index is not used 2/ PG13 is 3x slower one the same machine, same data.
addresses_pkey
is the primary key index. Don't think it can be dropped.explain analyse SELECT * FROM "addresses" AS a1 WHERE ST_DWithin('0101000020E61000001EE1B4E0455F0340E92807B309664840'::geography, a1."geom"::geography, '25000');
null
cases properly, run the cardinality query again asSELECT ST_SRID(geom) AS srid, count(*) from addresses group by 1;