基于百度/高德地图的一些应用经常会有这样一种场景的查询:
取X公里范围内最近的Y位司机/商户。
某公司是这样做的:司机有一张司机信息表DriverInfo,另有一张司机位置信息表DriverPosition。
DriverPosition会实时更新司机目前的经度和纬度。
获取X公里范围内的司机使用到了一个标量值函数GetDistance,传入客户的经度和纬度,以及司机的经度和纬度
结果返回两者之间的距离。那么典型的查询就是这样的:
DECLARE @longitude AS FLOAT = 31.2373453826; DECLARE @latitude AS FLOAT = 121.4976583979; DECLARE @Range AS TINYINT = 3; SELECT TOP 10 D.ID ,D.Phone ,GetDistance(@longitude,@latitude,DP.Longitude,DP.Latitude) AS Distance FROM DriverInfo AS D INNER JOIN DriverPosition AS DP ON D.ID = DP.ID WHERE GetDistance(@longitude,@latitude,DP.Longitude,DP.Latitude) <= @Range ORDER BY GetDistance(@longitude,@latitude,DP.Longitude,DP.Latitude) ASC
问题来了,上面这样的查询由于条件是在字段Longitude和Latitude上进行了运算,所以查询是会走Table Scan的。
大量的这样的并发查询会导致数据库服务器cpu一直爆表,怎么办呢?
--------------------------------------------------------------------------------------------------------------
实际上GetDistance这个函数无非是已客户的经纬度@longitude,@latitude为中心,以@Range为半径画了一个圆圈。
那么为了使用索引,我们也可以以@longitude,@latitude为中心,以@Range * 2为边,画一个正方形。如下图:
至于多少经度或者纬度是1公里呢,这里我们需要测算出一个系数x。例如近似0.009经度或纬度变化是1公里。
于是优化后的查询就成了下面这样:
DECLARE @longitude AS FLOAT = 31.2373453826; DECLARE @latitude AS FLOAT = 121.4976583979; DECLARE @Range AS TINYINT = 3; DECLARE @longitudeLow AS FLOAT = 31.2373453826 - 3*0.009; DECLARE @longitudeHight AS FLOAT = 31.2373453826 + 3*0.009; DECLARE @latitudeLow AS FLOAT = 121.4976583979 - 3*0.009; DECLARE @latitudeHight AS FLOAT = 121.4976583979 + 3*0.009; SELECT TOP 10 D.ID ,D.Phone ,GetDistance(@longitude,@latitude,DP.Longitude,DP.Latitude) AS Distance FROM DriverInfo AS D INNER JOIN DriverPosition AS DP ON D.ID = DP.ID WHERE DP.Longitude BETWEEN @longitudeLow AND @longitudeHightAND DP.Latitude BETWEEN @latitudeLow AND @latitudeHight AND GetDistance(@longitude,@latitude,DP.Longitude,DP.Latitude) <= @Range ORDER BY GetDistance(@longitude,@latitude,DP.Longitude,DP.Latitude) ASC
时间: 2024-09-30 04:53:04