问题
given one point A, get the nearest 100 points from one million data records
- database is MySql
- one million records of latitude and longitude
- these points mean user's current position when logined , so they may be changing.
scenario:
when a user open a page , display the nearest top 100 other people.
回答1:
- Setup spatial extensions for your database, if you have not done that already.
- Store latitude/longitude of your 1M locations in a geography-type column in the database.
- Create a spatial index on that column.
- Run a SELECT query with a WHERE clause based on distance between your point of interest and locations in the table. The query will utilize the above index.
Here is a good article on using spatial extension in MySQL 5.6 for exactly this sort of things.
回答2:
http://en.wikipedia.org/wiki/Geohash might be a quick way to speed up the average case, but worst case behaviour would still be bad. The article suggests that you index by geohash and, on a query, retrieve all points in a bounding box that amounts to a geohash prefix. If the bounding box is small and you find a match in it closer than any point outside the bounding box then you have succeeded quickly, but neither of these things might be true.
来源:https://stackoverflow.com/questions/25478006/how-to-get-the-nearest-100-points-from-one-million-data-records-quickly