因此,我的数据库有一个Id为BigInt的表,还有一个类型几何的表。几何字段有一个名为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我得到了错误消息:
查询处理器无法为具有空间索引提示的查询生成查询计划。原因:空间索引不支持谓词中的比较和提供。尝试删除索引提示或删除设置的FORCEPLAN。
就我而言,这两个查询基本上是等价的。有人能解释为什么会发生这种情况,以及如何(或者如果)让索引处理第二个查询吗?
谢谢
编辑:刚刚注意到第二个在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:我已经为我的问题找到了一个关于左联接和空检查的工作,但是我仍然好奇为什么如果有人能启发我,为什么不能在一个错误的相交上使用一个索引
发布于 2011-02-25 16:39:17
空间索引不能支持这个查询没有技术上的原因,但是生成的查询计划与用左反半连接自己执行查询计划本质上是一样的。支持这一点是考虑过的,但是这样做需要对查询优化器进行额外的更改,以便匹配这个谓词并生成正确的查询计划。
因此,考虑到这不是一个常见的查询形状,而且编写查询以自己使用索引仍然相对容易,这种模式没有包含在空间索引的支持的谓词列表中。
https://stackoverflow.com/questions/4957998
复制相似问题