SQL 2003距离纬度经度

发布于 2024-08-25 17:14:44 字数 698 浏览 4 评论 0原文

我有一桌子的经销商以及他们的纬度和经度。我正在尝试确定与任何给定经纬度最接近的前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2024-09-01 17:14:44

您可以对纬度/经度数字使用简单的毕达哥拉斯计算并选择前 20 个记录,而不是对所有记录执行计算。然后您可以使用更准确的函数选择实际的前 10 个记录。虽然一开始这是不准确的,但它应该足够准确以减少您的数据集。

编辑:类似这样的

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 
WHERE
    MemberId IN
(
    SELECT TOP 20
        MemberID
    FROM
        Dealers
    ORDER BY
        SQRT(SQUARE(@Lat - Lat) + SQUARE(@Lon - Lon))
)
Order by 
    (SELECT fun_DistanceLatLon] (@Lat,@lon,Lat,Lon)) 

但是,我建议您尝试使用您的数据集并对其进行分析,以查看实际的性能差异可能是什么。

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

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 
WHERE
    MemberId IN
(
    SELECT TOP 20
        MemberID
    FROM
        Dealers
    ORDER BY
        SQRT(SQUARE(@Lat - Lat) + SQUARE(@Lon - Lon))
)
Order by 
    (SELECT fun_DistanceLatLon] (@Lat,@lon,Lat,Lon)) 

However, I suggest you try both with your dataset and profile it to see what the actual performance difference may be.

雪化雨蝶 2024-09-01 17:14:44

我认为您真的很喜欢地理空间索引。否则,随着行数变得非常大,您将必须访问每一行才能进行毕达哥拉斯计算。

看起来 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文