SQL超时地理查询

时间:2012-09-18 20:38:39

标签: sql tsql sql-server-2008-r2 sql-server-profiler sqlgeography

我在此查询上获得随机/间歇性SQL超时,看起来它从简单查询生成大量处理。这是正确的行为吗?

我有一个像这样的简单存储过程:

CREATE PROCEDURE [dbo].[FindClosestXNearCoordinates]
      @latitude decimal,
      @longitude decimal
   AS
BEGIN

SET NOCOUNT ON;
declare @emptyGUID uniqueidentifier
set @emptyGUID = cast(cast(0 as binary) as uniqueidentifier)
declare @radiusInMeters float
set @radiusInMeters = 3500 * 1609.344   
declare @coordinatePoint as Geography   
SET @coordinatePoint = geography::STGeomFromText('POINT(' + CAST(@longitude AS VARCHAR(20)) + ' ' + CAST(@latitude AS VARCHAR(20)) + ')', 4326)
declare @coordinateRadius as Geography
set @coordinateRadius = @coordinatePoint.STBuffer(@radiusInMeters);


select  top 1   [b].[BaseId], [b].[Code], [b].[Name], [b].[Location], [b].[TerritoryId], [b].[Latitude], [b].[Longitude]
from        XTable b
where       ( b.GeoLocation.STIntersects(@coordinateRadius) = 1 )
order by b.GeoLocation.STDistance(@coordinatePoint) asc

END

我在SQL事件探查器中捕获它并且它连续显示查询和超过188个语句,这实在令人困惑,因为当我在SSMS中运行它时它只显示1次执行,但在应用程序中运行会生成188个子语句:

1 个答案:

答案 0 :(得分:0)

首先是SQL事件探查器。因为Spatial类型对于SQL Server来说有点特殊,所以它们会产生额外的处理。它为同一个TSQL语句多次注册SP:Starting和SP:Completed条目,因为它只能报告TSQL级别的操作。你将不得不忍受这个。在SQL Server 2012中也是如此。

关于你的查询超时,我建议用更基本的测试替换STIntersect(),使条件

where (b.longitude between @longitude-@xdelta and @longitude+@xdelta)
  and (b.latitude between @latitude-@ydelta and @latitude-@ydelta)
order by b.GeoLocation.STDistance(@coordinatePoint) asc

关键是通过将米转换为十进制值来找出合适的@xdelta。转换让我感到震惊,但谷歌是你的朋友。如果确实需要,你甚至可以添加STIntersect(缓冲区)。