Tsql 空间数据库,奇怪的索引行为

Tsql 空间数据库,奇怪的索引行为,tsql,sql-server-2008,spatial,spatial-index,Tsql,Sql Server 2008,Spatial,Spatial Index,因此,我的数据库有一个Id为BigInt类型的表和一个Geometry类型的几何体。Geometry字段有一个称为idx_Geometry的空间索引 使用索引时,以下查询按预期工作: DECLARE @Geometry geometry SET @Geometry = geometry::Parse('Polygon((300000 300000, 300000 325000, 325000 325000, 325000 300000, 300000 300000))') SELECT Id F

因此,我的数据库有一个Id为BigInt类型的表和一个Geometry类型的几何体。Geometry字段有一个称为idx_Geometry的空间索引

使用索引时,以下查询按预期工作:

DECLARE @Geometry geometry
SET @Geometry = geometry::Parse('Polygon((300000 300000, 300000 325000, 325000 325000, 325000 300000, 300000 300000))')
SELECT Id FROM [Database].[dbo].[Table] WITH(index(idx_Geometry)) WHERE Geometry.STIntersects(@Geometry) = 1
但是,当我尝试查询时

DECLARE @Geometry geometry
SET @Geometry = geometry::Parse('Polygon((300000 300000, 300000 325000, 325000 325000, 325000 300000, 300000 300000))')
SELECT a.Id FROM [Database].[dbo].[Table] a with(Index(idx_Geometry)) WHERE a.Geometry.STIntersects(@Geometry) = 0
我收到错误消息:

查询处理器无法生成 具有 空间索引提示。原因:空间 索引不支持比较项 在谓词中提供。尝试 删除索引提示或删除 制定计划

就我而言,这两个查询基本上是等价的。有人能解释为什么会发生这种情况,以及我如何(或者如果)让索引处理第二个查询吗

谢谢

编辑:刚刚注意到where子句中的第二个是=0,而不是=1,有人知道为什么索引不能与=0一起使用吗?(第二个查询使用=1)

编辑2:只需更新哪些有效,哪些无效

DECLARE @Geometry geometry
SET @Geometry = geometry::Parse('Polygon((300000 300000, 300000 325000, 325000 325000, 325000 300000, 300000 300000))')

--Works
SELECT Id FROM [RoadRoutingDatabase].[dbo].[Node] WITH(index(idx_Geometry)) WHERE Geometry.STIntersects(@Geometry) = 1
SELECT a.Id FROM [RoadRoutingDatabase].[dbo].[Node] a with(Index(idx_Geometry)) WHERE a.Geometry.STIntersects(@Geometry) = 1 

--Gives Error Message
SELECT Id FROM [RoadRoutingDatabase].[dbo].[Node] WITH(index(idx_Geometry)) WHERE Geometry.STIntersects(@Geometry) = 0
SELECT a.Id FROM [RoadRoutingDatabase].[dbo].[Node] a with(Index(idx_Geometry)) WHERE a.Geometry.STIntersects(@Geometry) = 0

--Works but doesn't use Index
SELECT Id FROM [RoadRoutingDatabase].[dbo].[Node] WHERE Geometry.STIntersects(@Geometry) = 0
SELECT a.Id FROM [RoadRoutingDatabase].[dbo].[Node] a WHERE a.Geometry.STIntersects(@Geometry) = 0 

编辑3:我已经找到了解决我的问题的方法,使用左连接和空检查,但我仍然很好奇,如果有人能告诉我,为什么你不能在错误的相交上使用索引

没有技术原因空间索引不能支持此查询,但是,生成的查询计划基本上与使用左反半联接自己进行查询相同。我们考虑了支持这一点,但这样做需要对查询优化器进行额外的更改,以便匹配此谓词并生成正确的查询计划


因此,考虑到这不是一个常见的查询形状,而且编写查询以自己使用索引仍然相对容易,这个模式没有包含在for space index中

但是我不明白,Intersect查询是否基本相同,只是查询结果的检查不同,为什么这会影响是否可以使用提示