I have (what I think) is a simple SQL Server spatial query:
Grab all the USA States that exist inside some 4 sided polygon (ie. the viewport/bounding box of a web page's Google/Bing map)
SELECT CAST(2 AS TINYINT) AS LocationType, a.Name AS FullName,
StateId, a.Name, Boundary.STAsText() AS Boundary,
CentrePoint.STAsText() AS CentrePoint
FROM [dbo].[States] a
WHERE @BoundingBox.STIntersects(a.Boundary) = 1
It takes 6 seconds to run, which is too slow.
How can I debug this, or what do I need to fine tune it? Do I have any spatial indexes? I believe so.
/****** Object: Index [SPATIAL_States_Boundary]
Script Date: 07/28/2010 18:03:17 ******/
CREATE SPATIAL INDEX [SPATIAL_States_Boundary] ON [dbo].[States]
(
[Boundary]
)USING GEOGRAPHY_GRID
WITH (
GRIDS =(LEVEL_1 = HIGH,LEVEL_2 = HIGH,LEVEL_3 = HIGH,LEVEL_4 = HIGH),
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
Do I need to provide some more information on the GEOGRAPHY
data which is returned? eg. number of points, etc? Or do I need to run profiler
and give some stats from there?
Or are my Cells_per_object / Grids set incorrectly ( I really have no idea what I should be setting those values to, to be honest).
Update
After the first reply from @Bobs below confirming that the spatial index was not getting used because the Primary Key (clustered Index) would be faster than a non-clustered index on a table with 50 odd rows, I then tried to force the Spatial Index:
SELECT CAST(2 AS TINYINT) AS LocationType, a.Name AS FullName,
StateId, a.Name, Boundary.STAsText() AS Boundary,
CentrePoint.STAsText() AS CentrePoint
FROM [dbo].[States] a WITH (INDEX(SPATIAL_States_Boundary))
WHERE @BoundingBox.STIntersects(a.Boundary) = 1
Now the query runs instantly. So my new question is, why did that fix it?