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 开发者_如何学JAVAusing 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.
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.
Why do you think that the issue is in the ON
clause? You're using function in the WHERE
part applied to the b
table - that is why you have fullscan over your a
table, and partial scan over b
table.
精彩评论