我试图找到找到一个点到点表的最小距离的最快方法。唯一需要注意的是,我试图找到最小距离的点表是 150K 单点。
或者更好地解释表 A 有 150K 行/点,表 B 1500 点。我想知道表 A 中的每一行与表 B 中列出的所有行的最小距离是多少。
我有一个进行距离计算的函数,作为表 A 的附加列。它只需要很长时间。表 B 有一个空间索引。
这就是我所拥有的:
select a.*,
dbo.fxn_distance(geography::STPointFromText('POINT(' +
CAST([Long] AS VARCHAR(20)) + ' ' + CAST([Lat] AS
VARCHAR(20)) + ')', 4326)) as DistAway
from Table A a
Run Code Online (Sandbox Code Playgroud)
我的功能:
create function fxn_distance
(@pointTableA geography
)
returns float
as
begin
declare @distance float
select top 1 @distance = b.GeoLocation.STDistance(@pointTableA)
from TableB b
where geolocation.STDistance(@pointTableA) is not null
order by geolocation.STDistance(@pointTableA)
return @distance
end
Run Code Online (Sandbox Code Playgroud)
对不起,如果我完全是一个新手,我知道解决方案可能很简单,但我无法解决这个问题。所以希望澄清一下:我需要通过表 A …