views:

52

answers:

2

I'm trying to design a MySQL schema that can store a list of users with an associated latitude and longitude.

I would then, for a given user, like to build a query which can return the nearest 50 users to him/her and sort those users by distance (with the nearest being presented first).

Given that there may be many thousands of users in this table, what is the most efficient way to store and query this data?

+3  A: 

Take a look at MySql's spatial indexing.

You can also use the Great Circle Distance, there is a good SO article on it here.

Abe Miessler
+3  A: 

Try reading this article: Creating a Store Locator with PHP, MySQL & Google Maps That article shows a MySQL solution for the Haversine formula, which is the best way to compute distance given latitude and longitude.

Bill Karwin
Bill are you StackOverflow stalking me? You've been answering all the same posts as me today....
Abe Miessler
Nope, it's just that great minds think alike! :)
Bill Karwin