I just found a very cool related trick.

Suppose you need to find all locations that are within 'n' kilometers of [x,y]. The "mostly accurate" way (ignoring the latitude problem) to do it is to "select * from locations where sqrt((x-@x)*(x-@x)+(y-@y)*(y-@y)) < @n". However, that's very slow, as you're doing math on every row of the database.

instead, you search within a bounding rectangle which is the same height/width as the n*2. This involves simple < and > checks. The result set from that will be very small, allowing you to do the accurate query on just the results. Very fast indeed.

If you have a recent MySQL (or any other db), you can do the bounding rectangle query as a subquery of the main one, letting you do a very fast check in one line!

The original article is here:

Arjen's Journal - Great circle and distances