I have a bit of a performance issue with my script, so I wonder if anyone knows a better way to approach this.
I have a database with locations and their postcodes(id, name, postcode). In another table I have latitude and longitude associated to postcodes(lat, long, postcode). The script asks user for a postcode and returns 3 closest locations.
The way I’m currently doing this, is first: I’m getting all the locations from db to a local array, then using a mysql query I’m getting distances between location and user’s postcode for each location.
SELECT SQRT( POW( (
uc1.lat - uc2.lat
), 2 ) + POW( (
uc1.long - uc2.long
), 2 ) )
FROM postcodes uc1
LEFT JOIN postcodes uc2 ON uc1.postcode = "$postcode1"
WHERE uc2.code = "$psotcode2"
After that I sort the array and display first 3 elements.
This works fine if number of locations is small, but as they approach thousands performance becomes an issue.
Any ideas?
EDIT:
I’m quite aware of curvature of the earth. I posted this query instead of more mathematically heavy one, so that it would be easier to understand what actually happens
Create a “bounding box” to use in a WHERE clause in your SQL query as described in this article on Movable Type (with PHP code examples), then include the Haversine formula in your query to calculate the actual distances, and order the result by distance ASC.
It’s the bounding box that helps your performance, because it means you’re only doing the expensive distance calculation on a small subset of your data