Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to get points within radius of a point with sql server spatial

I am trying to work out the most efficient query to get points within a radius of a given point. The results do not have to be very accurate so I would favor speed over accuracy.

We have tried using a where clause comparing distance of points using STDistance like this (where @point and v.GeoPoint are geography types):

WHERE v.GeoPoint.STDistance(@point) <= @radius

Also one using STIntersects similar to this:

WHERE @point.STBuffer(@radius).STIntersects(v.GeoPoint) = 1

Are either of these queries preferred or is there another function that I have missed?

like image 773
Luke Lowrey Avatar asked Jun 16 '11 12:06

Luke Lowrey


1 Answers

If accuracy is not paramount then using the Filter function might be a good idea: http://msdn.microsoft.com/en-us/library/cc627367.aspx

This can i many cases be orders of magnitude faster because it does not do the check to see if your match was exact. In the index the data is stored in a grid pattern, so how viable this approach is probably depends on your spatial index options.

Also, if you don't have to many matches then doing a filter first, and then doing a full intersect might be viable.

like image 185
Tomas Avatar answered Sep 20 '22 13:09

Tomas