I have a SQL table : SuburbId int, SubrubName varchar, StateName varchar, Postcode varchar, Latitude Decimal, Longtitude Decimal
and in MY C# I have created code that creates a bounding box so I can search by distance. And my stored procedure to get the subrubs is:
[dbo].[Lookup] (
@MinLat decimal(18,15),
@MaxLat decimal(18,15),
@MinLon decimal(18,15),
@MaxLon decimal(18,15)
)
AS
BEGIN
SELECT SuburbId, suburbName, StateName, Latitude, Longitude
FROM SuburbLook开发者_如何转开发up
WHERE (Latitude >= @MinLat AND Latitude <= @MaxLat AND Longitude >= @MinLon AND Longitude <= @MaxLon)
END
My Question is.. this is a Clustered Index Scan... Is there a more efficient way of doing this?
This type of query tends to perform quite poorly with the standard B-tree index. For better performance you can use the geography
column type and add a spatial index.
Queries such as WHERE geography.STDistance(geography2) < number
can use a spatial index.
Couple of links that should help. Of course depending on the scope of you project you may already have the best solution.
That said if you care to, you can create a custom index in sql server for your locations.
Custom Indexing
Additionally if you wanted to, you could look into Quadtrees and Quadtiles. There is a technique where you can calculate a key via an interleaved addresses, a combination of the lat and lon pairs that, can be represented as an integer and then truncating to a base level to see how they relate to eachother.
see more here
精彩评论