我不确定我能得到一个明确的问题名称......
我想要的是计算点和多边形之间的距离(这是第1步,然后,对于每个点,只获得最接近的多边形(nb:一个多边形可以附加许多点,但必须附加一个点)只有一个多边形)。
我目前正在做的事情如下:
CREATE TABLE temp_table AS
SELECT
areas.*
points.* -- includes a points_id column
ST_DistanceSphere(areas.geometry, points.geometry) AS distance_sphere
FROM points
INNER JOIN areas
ON st_DWithin(areas.geometry, points.geometry, 25)
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY temp_table.points_id ORDER BY distance_sphere ASC) as rownumber, *
FROM temp_table
) X
WHERE rownumber = 1
我觉得它效率很低(第一个请求已经整夜处理,在一个4 000 000行的数据库中......最后需要29百万,最后一个limit 10
)因为它& #39;计算许多无用的行。
将第一个请求放在第二个请求会更快吗?
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY temp_table.points_id ORDER BY distance_sphere ASC) as rownumber, *
FROM (
SELECT
areas.*
points.* -- includes a points_id column
ST_DistanceSphere(areas.geometry, points.geometry) AS distance_sphere
FROM areas
INNER JOIN points
ON st_DWithin(areas.geometry, points.geometry, 25)
)
) X
WHERE rownumber = 1
如果没有,我怎样才能优化我正在做的事情?