SQL 2003距离纬度经度
我有一桌子的经销商以及他们的纬度和经度。我正在尝试确定与任何给定经纬度最接近的前 n 个经销商。我已经有了计算位置之间距离的函数,但我想尽可能少地进行计算(我的表可以包含数千个条目)。目前我必须计算每个条目的距离然后对它们进行排序。有没有办法在计算之前进行排序以提高性能?
这个问题很好,但是我并不总是知道我的范围。我应该选择一个任意高的范围然后完善我的结果吗?我感谢社区可以提供的任何帮助。
declare @Lat real
declare @lon real
Set @lat = 41.05
Set @lon = -73.53
SELECT top 10
MemberID,
Address1,
City,
State,
Zip,
Phone,
Lat,
Lon,
(SELECT fun_DistanceLatLon] (@Lat,@lon,Lat,Lon)) as mDistance --Calculate distance
FROM
Dealers
Order by
(SELECT fun_DistanceLatLon] (@Lat,@lon,Lat,Lon))
I have a table full of Dealers along with their latitude and longitude. I am trying to determine the top n closest dealers to any given lat and lon. I already have the function to calculate distance between locations, but I want to do as few calculations as possible (my table can contain many thousands of entries). Currently I have to calculate the distance for each entry then sort them. Is there any way to sort before I do the calculation to improve performance?
This question is good, but I will not always know my range. Should I just pick an arbitrarily high range then refine my results? I am thankful for any help the community can offer.
declare @Lat real
declare @lon real
Set @lat = 41.05
Set @lon = -73.53
SELECT top 10
MemberID,
Address1,
City,
State,
Zip,
Phone,
Lat,
Lon,
(SELECT fun_DistanceLatLon] (@Lat,@lon,Lat,Lon)) as mDistance --Calculate distance
FROM
Dealers
Order by
(SELECT fun_DistanceLatLon] (@Lat,@lon,Lat,Lon))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对纬度/经度数字使用简单的毕达哥拉斯计算并选择前 20 个记录,而不是对所有记录执行计算。然后您可以使用更准确的函数选择实际的前 10 个记录。虽然一开始这是不准确的,但它应该足够准确以减少您的数据集。
编辑:类似这样的
但是,我建议您尝试使用您的数据集并对其进行分析,以查看实际的性能差异可能是什么。
Instead of performing your calculation on all the records, you could use a simple pythagoras calculation on your lat / long numbers and select the top 20. Then you can select the actual top 10 using your more accurate function. Whilst this is inaccurate to start with it should be accurate enough to reduce your dataset.
EDIT: Something like this
However, I suggest you try both with your dataset and profile it to see what the actual performance difference may be.
我认为您真的很喜欢地理空间索引。否则,随着行数变得非常大,您将必须访问每一行才能进行毕达哥拉斯计算。
看起来 SQL Server 支持空间数据类型,SQL Server 2008 甚至夸耀“用于高性能查询的新空间索引"。您可以使用 SQL Server 空间数据类型,然后针对空间索引执行邻近查询吗?
I think you'd really like a geospatial index. Otherwise, as the number of rows grows very large, you'll have to access every single row to do the Pythagorean calculation.
It looks like SQL Server supports spatial data types, and SQL Server 2008 even boasts of "new spatial indexes for high performance queries". Could you use a SQL Server spatial data type and then do a proximity query against the spatial index?