2

I was working with SQL Server 2008 Spatial Data, but I got a weird problem that the Spatial Index created for the tables doesn’t work when I query them with the view which is created based on this table. Following is the scripts I was using:

 declare @Geometry1 geometry = geometry::STGeomFromText(
'POINT(937767.89433333278 -230404.864666667)', 102003)

exec sp_executesql 
N'SELECT shape FROM view WHERE (@Geometry1.STIntersects(SHAPE)=1);',
 N'@Geometry1 geometry', @Geometry1

I googled a lot and found an workaround at http://www.sqlskills.com/blogs/bobb/how-to-ensure-your-spatial-index-is-being-used/ , but seems like this workaround just works when the queried geometry is point type, for polygons, like the script as following:

declare @Geometry2 geometry = geometry::STGeomFromText(
'POLYGON((-2079214.0399 1392052.275,-2079214.0399 -1156112.025,
1981332.1069 -1156112.025,1981332.1069 1392052.275,
-2079214.0399 1392052.275))', 102003)

exec sp_executesql 
N'SELECT shape FROM view WHERE (@Geometry2.STIntersects(SHAPE)=1);',
 N'@Geometry2 geometry', @Geometry2

The spatial still doesn’t work. Anybody knows how to deal with this situation? Seems like the Microsoft doesn’t give a good instructions about this. Any response will be appreciated.

4

1 回答 1

1

请使用诊断存储过程sp_help_spatial_geography_index_xml 并查看其输出。

这是您可以用来了解如何使用地理空间索引的主要存储过程。

鉴于您的查询非常简单,@query_sample 参数sp_help_spatial_geography_index_xml 可以与您的 @geometry 参数相同。这将告诉您一些相当有用的信息,例如空间索引是否需要全面扫描来评估问题。

需要指出的另一件事:动态管理对象视图sys.dm_db_missing_index_details在文档中明确指出它不支持空间索引,因此 SQL Server 没有围绕这些索引提供很多智能。

于 2018-11-14T17:42:32.943 回答