2010-11-11 111 views
3

我有一列地理类型。我已创建AA空间索引,但其不被使用:未使用SQL Server 2008空间索引

declare @geog geography 
select @geog = 'POINT (18.12 -33.2)' 

select * 
from dbo.product WITH(INDEX(IX_Spatial)) 
where 
@geog.STDistance(GeoLocation) > 1000 

索引创建这样的:

CREATE SPATIAL INDEX [IX_Spatial] ON [dbo].[Product] 
(
[GeoLocation] 
)USING GEOGRAPHY_GRID 
WITH (
GRIDS =(LEVEL_1 = MEDIUM,LEVEL_2 = MEDIUM,LEVEL_3 = MEDIUM,LEVEL_4 = MEDIUM), 
CELLS_PER_OBJECT = 1024, 
PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, 
DROP_EXISTING = OFF, ALLOW_ROW_LOCKS = ON, 
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
GO 

为网格线密度分布的值在各个层次中不故意设置为中等。如果我查看估计的执行计划,则不会使用该索引。

[http://blogs.msdn.com/b/isaac/archive/2008/08/29/is-my-spatial-index-being-used.aspx][1]

如果我尝试将提示添加到查询优化器

declare @geog geography 
select @geog = 'POINT (18.12 -33.2)' 

select * 
from dbo.product WITH(INDEX(IX_Spatial)) 
where 
@geog.STDistance(GeoLocation) > 1000 

我得到这个错误:

The query processor could not produce a query plan for a query with a spatial index hint. Reason: Spatial indexes do not support the comparator supplied in the predicate

我的数据库是在SQL Server 2008中(100)的兼容性级别运行。

回答

2

SQL Server通过以下方式创建空间索引:将整个地图拆分为若干个矩形(tiles)并索引每个图块的编号。

对于任何给定的点,所以能够内计算矩形的数量的距离而不是出的距离的(可以有无限多的)。

+0

这解释了为什么使用少于使用索引。谢谢 – 2010-11-11 12:44:11