how to get the nearest 100 points from one million data records quickly?

末鹿安然 提交于 2019-12-09 03:38:42

问题


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:


  1. Setup spatial extensions for your database, if you have not done that already.
  2. Store latitude/longitude of your 1M locations in a geography-type column in the database.
  3. Create a spatial index on that column.
  4. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!