【问题标题】:Search nearby points from a geography column从地理列中搜索附近的点
【发布时间】:2012-04-17 13:45:25
【问题描述】:

我的表中有一个带有空间索引的地理类型列。如何在利用索引提高性能的同时选择给定纬度/经度 X 米内的前 N ​​行?

【问题讨论】:

    标签: sql geolocation sql-server-2008-r2 sqlgeography


    【解决方案1】:

    当您说“同时使用索引”时,您是什么意思?

    我才刚刚开始(今天)尝试使用 SQL 2012 及其 Geography 数据类型,但也许以下内容很有用 - 我知道您正在使用 2008-R2

    创建一些数据:

    CREATE TABLE SpatialTable 
        ( id int IDENTITY (1,1) PRIMARY KEY,
        GeogCol1 geography, 
        GeogCol2 AS GeogCol1.STAsText() );
    GO
    
    INSERT INTO SpatialTable (GeogCol1)
    VALUES 
    (geography::STGeomFromText('POINT(-122.360 47.656)',4326)),
    (geography::STGeomFromText('POINT(-122.343 47.656)',4326)),
    (geography::STGeomFromText('POINT(-122.358 47.660)',4326)),
    (geography::STGeomFromText('POINT(-122.348 47.649)',4326)),
    (geography::STGeomFromText('POINT(-122.348 47.658)',4326)),
    (geography::STGeomFromText('POINT(-122.358 47.653)',4326))
    

    创建索引:

    CREATE SPATIAL INDEX Spatialindex ON SpatialTable ( GeogCol1 ) USING GEOGRAPHY_GRID 
    WITH (
        GRIDS =(LEVEL_1 = MEDIUM,LEVEL_2 = MEDIUM,LEVEL_3 = MEDIUM,LEVEL_4 = MEDIUM),
        CELLS_PER_OBJECT = 16, PAD_INDEX  = OFF, SORT_IN_TEMPDB = OFF,
        DROP_EXISTING = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON
    )
    

    定义兴趣点:

    DECLARE @g geography;
    SET @g = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
    

    在点的 750m 内找到最近的 2

    SELECT TOP 2
            @g.STDistance(st.GeogCol1) AS [DistanceFromPoint (in meters)] 
        ,   st.GeogCol2
        ,   st.id
    FROM    SpatialTable st WITH(INDEX(SpatialIndex))
    WHERE   @g.STDistance(st.GeogCol1) <= 750
    ORDER BY @g.STDistance(st.GeogCol1) ASC
    

    给予:

    DistanceFromPoint (in meters) GeogCol2                 id
    ----------------------------- ------------------------ -----------
    234.715604015178              POINT (-122.348 47.649)  4
    711.760044795868              POINT (-122.358 47.653)  6
    

    SQL Execution Plan 表示正在使用索引,this website 表示返回的距离是正确的

    如果这不是您需要的,请告诉我,我正在尝试自己学习,所以任何修订都很好!

    *** 编辑 **** 我添加了一个索引表提示来强制查询使用索引(根据这个MSDN post)。该链接解释了最初可能没有使用索引,因为 SQL 确定不使用索引会更快。我上面示例的执行计划显示使用/不使用索引的比例为 97%/3%,因此在这种情况下,SQL 优化器的想法是正确的。

    为什么使用索引时查询会变慢,这似乎是SO 上众多博客文章和问题的主题。

    【讨论】:

    • 看起来这就是它所需要的。我会检查这是否利用了我添加的空间索引。
    • 执行计划没有显示它正在使用空间索引
    • @Bahamut 根据 (blogs.msdn.com/b/isaac/archive/2008/08/29/…) 我添加了一个索引提示,它有效
    • 嘿,谢谢帕迪,这很有效,我用谷歌地图的实际纬度和经度验证了这一点。我唯一的问题是,这会返回我两点之间的实际距离,据我了解,它是空中距离......正确吗?
    • 来自hereSTDistance() returns the shortest LineString between two geography types. This is a close approximate to the geodesic distance. The deviation of STDistance() on common earth models from the exact geodesic distance is no more than .25%. This avoids confusion over the subtle differences between length and distance in geodesic types.
    猜你喜欢
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多