从 postgresql 中的 2 列中查找最接近的匹配项

Find closest match from 2 columns in postgresql

我有一个 table "ways" 包含坐标 (lat/lon) 值。假设我有一个坐标 (x,y) 并且我想从 table 方式中检查最接近的匹配。我见过一些类似的问题:Is there a postgres CLOSEST operator?

但是在我的例子中,我有 2 列而不是 1 列。是否有查询来执行这样的操作?

您可以将数据存储为 PostGIS 'point' 类型而不是坐标值:

这将使您能够使用所有 PostGIS 功能,例如:

然后您可以创建一个 GiST 索引并使用 PostGIS <-> operator 来利用索引辅助的最近邻结果集。 'nearest neighbor' 功能很常见。这是一个很好的解释:

“KNN” stands for “K nearest neighbours”, where “K” is the number of neighbours you are looking for.

KNN is a pure index based nearest neighbour search. By walking up and down the index, the search can find the nearest candidate geometries without using any magical search radius numbers, so the technique is suitable and high performance even for very large tables with highly variable data densities.