spring中mysql的坐标,数据库中的坐标比较

本文探讨了如何加速大量人员数据库中的GPS坐标搜索。建议使用边界矩形近似法先筛选出可能的匹配,再通过大圆距离公式精确计算距离。对于300k以上数据量的情况,可以考虑采用MySQL的地理空间索引方案。此外,还推荐了在不同精度需求下选择合适的距离计算公式,如Vincenty公式或Haversine公式。
摘要由CSDN通过智能技术生成

I am developing a Spring app that has to search for persons using GPS coordinates.

For each in the DB person I have his latitude and his longitude. The client pass a point and a max distance to the server which has to return all the clients around this point in this distance.

The problem is that the database contains about 300k persons, so getting all the persons and iterating through this list to chck if they are near the point is really slow.

Any idea to speed up this search ?

解决方案

The best way to handle proximity searches is to start with some kind of bounding-rectangle approximation, and then go from there to an actual great-circle distance between people.

As long as your your latitudes aren't too near the poles, a sloppy but workable approximation for the distance between two points is this (in SQLish):

GREATEST(ABS(lat1-lat2),ABS(long1-long2))

If you want to be more precise and you know you only care about people who are within, let us say, 10 km each other you can use a bounding rectangle search like this.

WHERE latitude_from_table

BETWEEN latpoint - (10.0 / 111.045)

AND latpoint + (10.0 / 111.045)

AND longitude_from_table

BETWEEN longpoint - (10.0 / (111.045 * COS(RADIANS(latpoint))))

AND longpoint + (10.0 / (111.045 * COS(RADIANS(latpoint))))

This works because there are 111.045 km in one degree of latitude. The cosine terms in the longitude bounds account for the fact that lines of latitude are closer together as you come near to the poles. This lets you exploit MySQL indexes on your latitude_from_table and longitude_from_table columns.

Once you have bounding-box proximity, you can apply a great circle distance formula. Here's background on that. http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

For the kind of application you are considering, 32-bit IEEE-488 floating point is plenty of precision for your coordinates. If the points you are looking at are really close together (less than a km or so) you want to use the Vincenty formula (http://www.plumislandmedia.net/mysql/vicenty-great-circle-distance-formula/) rather than the more common so-called haversine formula (http://www.plumislandmedia.net/mysql/stored-function-haversine-distance-computation/).

If your number of people goes much above 300K, you may want to consider using the MySQL geospatial indexing scheme. It only works with MyISAM tables, but it is very fast at doing bounding-rectangle searches. See here. http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值