Searching by distance in SQL
I am planning a website (Drupal/MySQL), which must search a fairly large database based on distance from a location (we're starting with ~20,000 locat开发者_运维百科ions). So far, the best solution I've found to searching in a reasonable manner is to use a user-defined function in SQL to calculate the distance between to coordinates, e.g.:
SELECT *, CoordinateDistanceMiles(lat, lon, ${inputLat}, ${inputLon}) as distance
FROM items WHERE distance < {$radius}
(Using John Dyer's distance function or similar)
However, I've also read that UDFs are very inefficient. My second idea (and tentative plan) is to nest another query inside this one to narrow its' scope and therefore run the UDF on a much smaller subset of items, e.g.:
SELECT *, CoordinateDistanceMiles(lat, lon, ${inputLat}, ${inputLon}) as distance
FROM (
SELECT * FROM items WHERE
lat BETWEEN ${inputLat - const} AND ${inputLat + const} AND
lon BETWEEN ${inputLon - const} AND ${inputLon + const}
) WHERE distance < ${radius}
Would this model make the search faster, or just more convoluted? Are there any better solutions?
The overhead of using UDF here is negligible, as long as you perform scan over distance < ${radius}
and have 2 range-based comparisons (they cannot be optimized with indexes).
So don't worry about UDF "inefficiency" and use it, since it is much more readable.
精彩评论