How can I specify this SQL Index Hint on my SQL Server 2008 query?

Pure.Krome picture Pure.Krome · Nov 15, 2010 · Viewed 19.3k times · Source

I'm not sure where/how I specify this SQL Spatial Index hint on my query :-

SELECT Whatever
FROM Table1 a
    INNER JOIN Table2 b ON a.Id = b.Id
WHERE @SomeBoundingBox.STIntersects(b.SomeGeographyShape) = 1

When I run the query, it's NOT using the Spatial Hint. Yes, I'm using the the latest version SQL Server 2008 r2 (v 10.5.1600.1).

So to compare the query speed by forcing the hint, I tried...

SELECT Whatever
FROM Table1 a
    INNER JOIN Table2 b WITH (INDEX(MySpatialIndex)) ON a.Id = b.Id
WHERE @SomeBoundingBox.STIntersects(b.SomeGeographyShape) = 1

and that worked but it was really poor performance. I was wondering if it was trying to use my hint to do the a.Id = b.Id join (which i don't want to use the hint, for).

Any suggestions?

UPDATE:

Added Query Plan. The bulk of the cost is the JOIN between the two tables. The Filter (where clause) takes up the 2nd most costly part.

alt text

Answer

Flipster picture Flipster · Nov 15, 2010

I would suggest NOT forcing the hint. There is a reason that SQL Server is choosing the execution plan that it is, and as you suspected, forcing another plan will almost always result in decreased performance.

Instead, what you want to do is examine your indexes and query execution plans in detail, follow each node of the execution plan (especially ones with a high relative cost) and see if you can do what SQL Server wants you to do in order to pick up your MySpatialIndex index without you forcing it to do so. Usually, it will just be a missing field from an index that will cause the execution plan to work out just as you wish... with no forcing necessary.