且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostGIS两组大型点之间的最小距离

更新时间:2023-02-10 11:16:34

您的查询速度很慢,因为它无需使用任何索引即可计算每个点之间的距离.您可以将其重写为使用 <-> 运算符,该运算符在条款.

Your query is slow because it computes the distance between every points without using any index. You could rewrite it to use the <-> operator that uses the index if used in the order by clause.

select a.id,closest_pt.id, closest_pt.dist
from tablea a
CROSS JOIN LATERAL
  (SELECT
     id , 
     a.geom <-> b.geom as dist
     FROM tableb b
     ORDER BY a.geom <-> b.geom
   LIMIT 1) AS closest_pt;