geohash st_distance st_distance_sphere 关系

转:https://blog.csdn.net/weixin_44310899/article/details/102584812

 

st_distance函数


  MySQL其实在很早就提供了这种存储经纬度及相关运算的功能,这种数据类型叫做空间数据类型,而对应的索引被称为空间索引,但由于MySQL之前的版本对InnoDB支持的并不是太好,所以使用的并不多。不过MySQL5.6和MySQL5.7对此进行了优化,添加了st_distance等相关函数来支持经纬度相关的计算。
  这里只来看一下st_distance函数的使用,其他相关的函数我会专门写一篇文章来学习。我们还是拿上面Google Maps所建的表来测试,来按照距离进行查询:

 

1 SELECT
2     s.*, 
3     (st_distance(point(lng, lat), point(-122.083235, 37.38714) ) * 111195) AS distance
4 FROM
5     markers s
6 ORDER BY
7     distance

 

*111195 这个地方应该是有问题的。因为这个值在不同纬度是不一样的。所以还是st_distance_sphere更准确一点。

 

SELECT p.id ,(ST_Distance (point (p.lng, p.lat),point(120.24846,30.24983))*111195) as distance,(ST_Distance_Sphere (point (p.lng, p.lat),point(120.24846,30.24983))) as distance2 FROM points p  having distance<10000 limit 0,500;

 

 

https://dev.mysql.com/doc/refman/8.0/en/spatial-relation-functions-object-shapes.html#function_st-distance

 

st_distance_sphere函数

其实,MySQL有提供直接查询结果是米的函数:st_distance_sphere,并且该函数的计算结果要比st_distance转换为米的结果更精确。不过该函数是MySQL5.7之后才引入的,5.7之前还是需要通过计算转换成米。更多可参考官方文档地址:
MySQL 5.7 ST_Distance_Sphere(g1, g2 [, radius])

 

https://dev.mysql.com/doc/refman/8.0/en/spatial-convenience-functions.html#function_st-distance-sphere

 

Geohash算法 

简单的说,就是把经纬度根据一定算法转成一个字符串,这个字符串的特点是,经纬度接近的点位,生成的字符串也类似,然后后期可以通过LIKE查询的方式来找到附近的点。具体可以通过下面这个文章了解。

 https://blog.csdn.net/hong2511/article/details/81329361

 

 

空间索引 - 各数据库空间索引使用报告

https://www.cnblogs.com/zhenbianshu/p/6817569.html

 

计算两点之间的距离的几个方法性能比较:差距挺大的。

https://www.jianshu.com/p/65114a9ecb0d

 

posted @ 2021-01-31 18:04  nanahome  阅读(1106)  评论(0编辑  收藏  举报