Mysql之st_distance_sphere计算两坐标点距离

2021-06-30 10:33
343
0

https://blog.csdn.net/wl_Honest/article/details/103841412

 

SELECT
 t.* 
FROM
 house_info t 
WHERE  TRUNCATE ( st_distance_sphere (point ( t.longitude, t.latitude ),point ( 113.8064049, 22.7300434 )), 2 ) < 3000

 

最近项目中需要计算一个坐标点与多个点的距离,发现用Python来实现效率很低。经同事推荐,将这些坐标点存入了Mysql数据库,然后用数据库自带的方法st_distance_sphere计算距离。经过比较发现确实效率提高了很多,特此记录一下。

注意:使用st_distance_sphere需要Mysql数据库版本为5.7及以上。

例子:

现数据库表中有如下坐标点数据:

需要找出与第一个坐标点(113.8064049, 22.7300434)相距小于3500米的坐标点(表中共有3500个点)。

sql实现:

select t.num,t.city,t.wgs84_lng,t.wgs84_lat,
  TRUNCATE(st_distance_sphere(point (113.8064049, 22.7300434),point(t.wgs84_lng,t.wgs84_lat)),2) as distance
    from moran_point t
    where t.city = '深圳' 
    HAVING distance > 0 and distance < 3500
    ORDER BY distance;
查询结果:

可以看到,共找出41个满足条件的坐标点,且耗时仅为0.066秒,效率提升非常多。 

全部评论