I am trying to connect each record from table 1 (10k records) with its nearest neighbor in table (20k records). I am hoping there is a solution that does not require me to iterate over table 1 and perform a KNN for each record.
I have tried to do a KNN index limiting by reasonable distance, and selecting distinct on table 1 id from that as a sub-query, but this requires me to order by the id number rather than the distance.
Any advice is greatly appreciated.
select distinct on (t1id) t1id, it2d, dist, name, street
from (
select t1id, it2d, dist, name, street
from (
select t1id, it2d, st_distance(t1geom, t2geom) as dist, name, street
from (
select t1.id as t1id,t1.geom as t1geom, t2.id as t2id, t2.geom as t2geom, t1.name, t2.street
from t1
join t2
on st_dwithin(t1.geom, t2.geom, 300)
where t1.seg is null
) as near
order by t1geom <-> t2geom
) as distOrdered
order by dist
) as idOrdered
order by t1id