0
I remembered to remove and recreate the index to eliminate the possibility that anything changed
gidrop index if exists incidents;
create index idx_geo on incidents using gist(geo);
Then this:
vacuum analyze incidents;
Here's the query that is still O(n^2) and impossibly slow. Note that I ran it with and without enable_seqscan. No difference.
SET enable_seqscan TO off;
drop table if exists BSC;
create table BSC(
most_recent_id int not null,
incident_id int not null
);
insert into BSC(most_recent_id, incident_id)
select *
from (
select
(select max(id)
from incidents i2
where i2.geo_mesh && i.geo_mesh
and ST_DWithin(i2.geo_mesh, i.geo_mesh, 0)
and i2.id in (select most_recent_id from temp_unique)
) as most_recent_id,
id as incident_id
from incidents i
where i.id in (select most_recent_id from temp_unique)
) t
where t.most_recent_id <> t.incident_id;
SET enable_seqscan TO on