优化边界框查询

时间:2017-05-30 11:23:21

标签: sql sql-server sqlgeography

我有一张GeocodedPoints表。我正在优化的这个查询试图拉出一个lat-long位置的匹配点。不幸的是它太慢了!

该表基本上是一个边界框列表和一个相应的地址。它还包含精确边界框的DBGeography,但考虑到它在SQL中的速度有多慢,我将其实现为.NET land并在那里查询DBGeography。

我的查询基本上会查看点是否在[由NESW指定]的边界框内并返回结果。

在我看来,这应该很快,但唉,它没有我想象的那么快。

我在边界和UTC上有一个非唯一的非聚集索引,如此

enter image description here

请注意,UTC是必需的,因为我们只返回在过去两周内修改过的结果。

我通过SQL Profiler工具运行了这个,这里有一些信息:

  • 该表中的500k行
  • 持续时间范围为每次通话250-350毫秒
  • 读取范围为5-20k

最后这是我使用的查询

exec sp_executesql N'SELECT 
    [Project1].[ID] AS [ID], 
    [Project1].[CENTER] AS [CENTER], 
    [Project1].[BOUNDS] AS [BOUNDS], 
    [Project1].[UTC_UPDATED] AS [UTC_UPDATED], 
    [Project1].[PLACE_ID] AS [PLACE_ID], 
    [Project1].[FORMATTED_ADDRESS] AS [FORMATTED_ADDRESS], 
    [Project1].[POST_CODE] AS [POST_CODE], 
    [Project1].[SOURCE] AS [SOURCE], 
    [Project1].[North] AS [North], 
    [Project1].[East] AS [East], 
    [Project1].[South] AS [South], 
    [Project1].[West] AS [West]
    FROM ( SELECT 
        [Extent1].[ID] AS [ID], 
        [Extent1].[CENTER] AS [CENTER], 
        [Extent1].[BOUNDS] AS [BOUNDS], 
        [Extent1].[UTC_UPDATED] AS [UTC_UPDATED], 
        [Extent1].[PLACE_ID] AS [PLACE_ID], 
        [Extent1].[FORMATTED_ADDRESS] AS [FORMATTED_ADDRESS], 
        [Extent1].[POST_CODE] AS [POST_CODE], 
        [Extent1].[SOURCE] AS [SOURCE], 
        [Extent1].[North] AS [North], 
        [Extent1].[East] AS [East], 
        [Extent1].[South] AS [South], 
        [Extent1].[West] AS [West]
        FROM [dbo].[HST_GEOCODE_POINTS] AS [Extent1]
        WHERE ([Extent1].[UTC_UPDATED] > @p__linq__0) AND ([Extent1].[North] >= @p__linq__1) AND ([Extent1].[East] >= @p__linq__2) AND ([Extent1].[South] <= @p__linq__3) AND ([Extent1].[West] <= @p__linq__4)
    )  AS [Project1]
    ORDER BY [Project1].[UTC_UPDATED] DESC, [Project1].[SOURCE] DESC',N'@p__linq__0 datetime2(7),@p__linq__1 float,@p__linq__2 float,@p__linq__3 float,@p__linq__4 float',@p__linq__0='2017-05-16 11:12:12.4425257',@p__linq__1=53.016466402998645,@p__linq__2=-1.715320912729779,@p__linq__3=53.016466402998645,@p__linq__4=-1.715320912729779

注意我的UTC目前是此查询中的第一个,但在索引中是最后一个。具有讽刺意味的是,这似乎使我的查询更快,尽管每次调用都会达到20k读数。

1 个答案:

答案 0 :(得分:0)

1)使用geography代替DBGeography来存储数据: https://docs.microsoft.com/en-us/sql/t-sql/spatial-geography/spatial-types-geography

2)在geography列上创建空间索引 https://docs.microsoft.com/en-us/sql/relational-databases/spatial/spatial-indexes-overview

3)使用your_geography.STIntersects(other_geography)函数或类似的

寻找您需要的行

不幸的是,你的空间索引中没有其他列(例如UTC_UPDATED)。

P.S。您也可以尝试geometry类型