Zip Code Radius Searches That Aren't Painful
August 28, 2006 2:11 PM
Subscribe
What is the best strategy to limit using processing resources in doing a zip code radius query? (explained inside)
Imagine a scenario where you had a number of MySQL database records with information, including a zip code, and a cooresponding latitude and longitude associated with that zipcode.
Imagine now that you wanted to allow someone to see all records that fall within a certain radius of their own zipcode. In other words, I input that I am located in 60661, and a PHP program determines which of the MySQL records are within 10 miles of 60661.
I have seen lots of PHP/MySQL radius scripts, and have a fully accurate and complete list of zip codes with latitudes and longitudes. So my question isn't about trying to find them.
My question is: What is the best strategy to limit using processing resources in doing a query like this? Is it to predetermine what zipcodes fall within X miles, then get all records with those zipcodes? Is it to simply compare distances for every record, selecting only those that are within X miles? Is it to somehow do the latitude/longitude trig inside a MySQL query?
There seem to be a lot of people marketing "solutions" for problems like these, but the problem can't be that complicated, and since the trig functions to determine the distance between two zip codes are free and prevalant, I'd rather not pay for a half. Any thoughts are appreciated.
posted by JakeWalker to computers & internet (12 comments total)
2 users marked this as a favorite
Yeah. Create a table containing zipcode-to-zipcode distances. Feel free to limit the number of records to those zipcodes within, say, 100 miles.
Then, select * from zipcode where zipcode_to_zipcode.distance < querydistance and zipcode_to_zipcode.first_zipcode=queryZipCode andbr> zipcode.zipcode = zipcode_to_zipcode.second_zipcode
I'm sure you're aware that this is a very error-prone search, since zipcodes are irregularly shaped and of different sizes.>
posted by lobrien at 2:39 PM on August 28, 2006 [1 favorite]