Sql 服务器 - 如何根据 Lat/Long 最小最大值查询地理列
Sql Server - How do I query a geography column based on Lat/Long min max
我有一个 table,其中包含一个存储 属性 位置的地理列。
我有一个类似这样的程序 -
PROCEDURE dbo.spt_sold_property_search
(
@latitude_min Decimal(9,6),
@latitude_max Decimal(9,6),
@longitude_max Decimal(9,6),
@longitude_min Decimal(9,6)
)
AS BEGIN
SET NOCOUNT ON
SELECT * FROM [dbo].[sold_property] AS p WITH(NOLOCK)
WHERE p.location ***is in the lat/long min max bounds***
END
在 where 子句中我需要什么来检查地理点是否在 Lat/Long 最小最大值的范围内?这是一个大型数据集,因此性能至关重要。
我是否应该创建地理 SQL 从边界输入代码并将其作为过程传递到 proc 中?
我也在考虑创建 2 个计算的 int 列 (lat/long),它们将在插入时创建,然后是简单的 < >,因为我听说这比地理查询更快。
如果您只需要纬度和经度在 max/min 值范围内,则使用 Lat 和 Long 属性:
SELECT * FROM [dbo].[sold_property] AS p WITH(NOLOCK)
WHERE p.location.Lat BETWEEN @latitude_min and @latitude_max
AND p.location.Long BETWEEN @longitude_min and @longitude_max
但是,在我看来,根据提供的坐标构建多边形然后使用 STWithin 方法检查 table 中的点是否在多边形内是正确的,例如这个:
DECLARE @g geography;
SET @g = geography::Parse('POLYGON (('+CONVERT(NVARCHAR(20),@latitude_min)+', '+
CONVERT(NVARCHAR(20),@latitude_max)+', '+CONVERT(NVARCHAR(20),@longitude_min)+', '+
CONVERT(NVARCHAR(20),@longitude_max)+'))');
SELECT * FROM [dbo].[sold_property] AS p WITH(NOLOCK)
WHERE @g.STWithin(p.location)
请注意,后面的查询可能不是 sargable。正如 Ben Thul 在下面提到的,空间索引可能支持 STWithin
我有一个 table,其中包含一个存储 属性 位置的地理列。
我有一个类似这样的程序 -
PROCEDURE dbo.spt_sold_property_search
(
@latitude_min Decimal(9,6),
@latitude_max Decimal(9,6),
@longitude_max Decimal(9,6),
@longitude_min Decimal(9,6)
)
AS BEGIN
SET NOCOUNT ON
SELECT * FROM [dbo].[sold_property] AS p WITH(NOLOCK)
WHERE p.location ***is in the lat/long min max bounds***
END
在 where 子句中我需要什么来检查地理点是否在 Lat/Long 最小最大值的范围内?这是一个大型数据集,因此性能至关重要。
我是否应该创建地理 SQL 从边界输入代码并将其作为过程传递到 proc 中?
我也在考虑创建 2 个计算的 int 列 (lat/long),它们将在插入时创建,然后是简单的 < >,因为我听说这比地理查询更快。
如果您只需要纬度和经度在 max/min 值范围内,则使用 Lat 和 Long 属性:
SELECT * FROM [dbo].[sold_property] AS p WITH(NOLOCK)
WHERE p.location.Lat BETWEEN @latitude_min and @latitude_max
AND p.location.Long BETWEEN @longitude_min and @longitude_max
但是,在我看来,根据提供的坐标构建多边形然后使用 STWithin 方法检查 table 中的点是否在多边形内是正确的,例如这个:
DECLARE @g geography;
SET @g = geography::Parse('POLYGON (('+CONVERT(NVARCHAR(20),@latitude_min)+', '+
CONVERT(NVARCHAR(20),@latitude_max)+', '+CONVERT(NVARCHAR(20),@longitude_min)+', '+
CONVERT(NVARCHAR(20),@longitude_max)+'))');
SELECT * FROM [dbo].[sold_property] AS p WITH(NOLOCK)
WHERE @g.STWithin(p.location)
请注意,后面的查询可能不是 sargable。正如 Ben Thul 在下面提到的,空间索引可能支持 STWithin