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.