我们将地理数据存储在" location“表中,并根据该列搜索给定输入的最近位置。下面的查询用于获取25英里内的最近位置,此查询需要4秒以上的时间来检索4000条记录。我们甚至在位置字段上创建了空间索引。
DECLARE @Distance INT
SET @Distance =25
DECLARE @h sys.GEOGRAPHY
SET @h =CONVERT(sys.GEOGRAPHY, 0xE6100000010C92B06F27119D4140111AC1C6F53554C0)
SELECT CenterLocationId,
[Location].Stdistance(@h) * Cast(0.000621371 AS FLOAT(53)) AS Distance
FROM [dbo].[CenterLocation]
WHERE [Location].Stdistance(@h) * Cast(0.000621371 AS FLOAT(53)) <= @Distance
AND IsDeleted = 0
ORDER BY [Location].Stdistance(@h) * Cast(0.000621371 AS FLOAT(53)) 有人能建议如何在sql server 2014中提高这种查询性能吗?
发布于 2015-11-11 16:07:31
有两件事,不能保证能解决问题,但肯定能有所帮助。它们都是相关的:
SELECT CenterLocationId,
[Location].Stdistance(@h) * Cast(0.000621371 AS FLOAT(53)) AS Distance
FROM [dbo].[CenterLocation]
WHERE [Location].Stdistance(@h) <= @Distance / Cast(0.000621371 AS FLOAT(53))
AND IsDeleted = 0
ORDER BY [Location].Stdistance(@h)也就是说,最好不要在WHERE子句中对依赖于列值的表达式执行数学运算-这样会迫使服务器对每一行执行该数学运算,并破坏使用索引的任何可能性。
同样,在ORDER BY子句中执行乘法也没有意义,因为乘法(乘以正数)不会改变顺序。
https://stackoverflow.com/questions/33645380
复制相似问题