[edit] – as pointed out to me in the comments I should have noted that I will be using a LAMP based architecture, meaning a MySQL database. Sorry about forgetting to mention this.
I have to create a PHP backend for an iPhone app. The app sends up the users coords and requests the closes 10 local users.
I’m just wondering what is the most efficient way to determine this, I dont want to have to scan the entire table of users, calculate the distance between their geo-coords and the target & order them from lowest to farthest.
Can anyone suggest a more elegant solution instead of scanning all the users? Thanks for your time.
You could bucket the users by approximate latitude and longitude (e.g. by whole degrees, or tenths of degrees, or whatever scale is appropriate to the distribution you’ve got) and then work outward from the querying user’s bucket.
Not sure the extra complexity is worth it, though, unless the brute force approach is really a performance hog.