我们有一个相当简单的表,我们试图用Entity Framework查询,看起来像这样:
CREATE TABLE [dbo].[EFBlkFireRisks](
[EFBlkFireRiskId] [int] IDENTITY(1,1) NOT NULL,
[EFStateId] [int] NOT NULL,
[Lat] [decimal](18, 8) NOT NULL,
[Lon] [decimal](18, 8) NOT NULL,
[AdjustedPremiumMultiplier] [decimal](18, 4) NOT NULL DEFAULT ((0)),
[GeoLocation] [geography] NULL,
CONSTRAINT [PK_dbo.EFBlkFireRisks] PRIMARY KEY CLUSTERED
(
[EFBlkFireRiskId] ASC
)
它在GeoLocation列上有一个地理空间索引,如下所示:
CREATE SPATIAL INDEX [IX_EFBlkFireRisks_Spatial] ON [dbo].[EFBlkFireRisks]
(
[GeoLocation]
)USING GEOGRAPHY_AUTO_GRID
我们试图在该表中找到最接近给定纬度/经度点的条目。
C#查询如下所示:
var geoPoint = SqlSchemaFunctions.GetDbGeography(lat, lon);
var query = ctx.BulkFireRisks
.Where(x => x.EFStateId == stateId && x.GeoLocation.Distance(geoPoint) != null)
.OrderBy(x => x.GeoLocation.Distance(geoPoint));
var bfr = query.First();
查询生成的SQL看起来基本上是这样的:
declare @p3 sys.geography set @p3=convert(sys.geography,0xE6100000010C8FC2F5285CFF434052B81E85EB515AC0)
declare @p4 sys.geography set @p4=convert(sys.geography,0xE6100000010C8FC2F5285CFF434052B81E85EB515AC0)
exec sp_executesql N'SELECT TOP (1)
[Project1].[EFBlkFireRiskId] AS [EFBlkFireRiskId],
[Project1].[EFStateId] AS [EFStateId],
[Project1].[GeoId] AS [GeoId],
[Project1].[Lat] AS [Lat],
[Project1].[Lon] AS [Lon],
[Project1].[GeoLocation] AS [GeoLocation],
[Project1].[AdjustedPremiumMultiplier] AS [AdjustedPremiumMultiplier],
[Project1].C1 as Distance
FROM ( SELECT
[Extent1].[GeoLocation].STDistance(@p__linq__1) AS [C1],
[Extent1].[EFBlkFireRiskId] AS [EFBlkFireRiskId],
[Extent1].[EFStateId] AS [EFStateId],
[Extent1].[GeoId] AS [GeoId],
[Extent1].[Lat] AS [Lat],
[Extent1].[Lon] AS [Lon],
[Extent1].[GeoLocation] AS [GeoLocation],
[Extent1].[AdjustedPremiumMultiplier] AS [AdjustedPremiumMultiplier]
FROM [dbo].[EFBlkFireRisks] AS [Extent1]
WHERE (11 = [Extent1].[EFStateId]) AND ([Extent1].[GeoLocation].STDistance(@p__linq__0) IS NOT NULL)
) AS [Project1]
ORDER BY [Project1].[C1] ASC',N'@p__linq__0 [geography],@p__linq__1 [geography]',@p__linq__0=@p3,@p__linq__1=@p4
该查询不使用索引 - 将其置于循环中并运行10次大约需要30秒(无论是通过SQL手动执行,还是通过C#从EF运行)
但是,可以删除sp_executesql
位,并直接运行查询,如下所示:
declare @p3 sys.geography set @p3=convert(sys.geography,0xE6100000010C8FC2F5285CFF434052B81E85EB515AC0)
SELECT TOP (1)
[Project1].[EFBlkFireRiskId] AS [EFBlkFireRiskId],
[Project1].[EFStateId] AS [EFStateId],
[Project1].[GeoId] AS [GeoId],
[Project1].[Lat] AS [Lat],
[Project1].[Lon] AS [Lon],
[Project1].[GeoLocation] AS [GeoLocation],
[Project1].[AdjustedPremiumMultiplier] AS [AdjustedPremiumMultiplier],
[Project1].C1 as Distance
FROM ( SELECT
[Extent1].[GeoLocation].STDistance(@p3) AS [C1],
[Extent1].[EFBlkFireRiskId] AS [EFBlkFireRiskId],
[Extent1].[EFStateId] AS [EFStateId],
[Extent1].[GeoId] AS [GeoId],
[Extent1].[Lat] AS [Lat],
[Extent1].[Lon] AS [Lon],
[Extent1].[GeoLocation] AS [GeoLocation],
[Extent1].[AdjustedPremiumMultiplier] AS [AdjustedPremiumMultiplier]
FROM [dbo].[EFBlkFireRisks] AS [Extent1]
WHERE (11 = [Extent1].[EFStateId]) AND ([Extent1].[GeoLocation].STDistance(@p3) IS NOT NULL)
) AS [Project1]
ORDER BY [Project1].[C1] asc
并且 查询不会运行30 秒,但是30 毫秒。
我在挠头。知道两个查询为什么有这么截然不同的执行模式?知道如何让EF使用这个特定的地理空间索引吗?我做错了什么?
编辑6/18 -
如果我尝试向较慢的sp_executesql
查询添加索引提示,则会收到以下错误消息:
查询处理器无法为具有空间索引提示的查询生成查询计划。原因:空间索引不支持谓词中提供的比较器。
相同的提示在另一个查询中起作用 - 或者至少不会出错 -
慢速查询的查询计划如下所示:
换句话说,它没有拿起地理空间索引。快速版的查询计划是获取索引,看起来像您期望的那样:
这是在SQL Server 2014上运行的:
(Microsoft SQL Server 2014 - 12.0.2000.8(X64) 2014年2月20日20:04:26 版权所有(c)Microsoft Corporation Windows NT 6.3(Build 9600:)
上的Express Edition(64位)
答案 0 :(得分:3)
好的,我明白了。
事实证明,这两个查询并不完全相同:sp_executesql
版本有两个参数,而快速版只有一个参数:这就是关键。这个查询(只有一个参数)就像你期望的那样执行:
declare @p3 sys.geography
set @p3=convert(sys.geography,0xE6100000010C8FC2F5285CFF434052B81E85EB515AC0)
exec sp_executesql N'SELECT TOP (1)
[Project1].[EFBlkFireRiskId] AS [EFBlkFireRiskId],
[Project1].[EFStateId] AS [EFStateId],
[Project1].[GeoId] AS [GeoId],
[Project1].[Lat] AS [Lat],
[Project1].[Lon] AS [Lon],
[Project1].[GeoLocation] AS [GeoLocation],
[Project1].[AdjustedPremiumMultiplier] AS [AdjustedPremiumMultiplier]
FROM ( SELECT
[Extent1].[EFBlkFireRiskId] AS [EFBlkFireRiskId],
[Extent1].[EFStateId] AS [EFStateId],
[Extent1].[GeoId] AS [GeoId],
[Extent1].[Lat] AS [Lat],
[Extent1].[Lon] AS [Lon],
[Extent1].[GeoLocation] AS [GeoLocation],
[Extent1].[AdjustedPremiumMultiplier] AS [AdjustedPremiumMultiplier],
[Extent1].[GeoLocation].STDistance(@p__linq__0) AS [C1]
FROM [dbo].[EFBlkFireRisks] AS [Extent1]
WHERE (11 = [Extent1].[EFStateId]) AND ([Extent1].[GeoLocation].STDistance(@p__linq__0) IS NOT NULL)
) AS [Project1]
ORDER BY [Project1].[C1] ASC',N'@p__linq__0 [geography]',@p__linq__0=@p3
在LINQ中获取该查询的方法如下:
var geoPoint = DbGeography.PointFromText(string.Format("POINT({0} {1})", lon, lat), 4326);
var q2 = (from b in ctx.BulkFireRisks
let distance = b.GeoLocation.Distance(geoPoint)
where b.EFStateId == stateId && distance != null
orderby distance
select b);
var bfr = q2.First();
关键是只定义一次sys.geography
变量。