小编Mat*_*ran的帖子

在 MySQL 8 中使用点数据类型和 st_distance_sphere 查找最近的地方

我有一张桌子叫place

id | name       | coordinates (longitude, latitude)
1  | London     | -0.12574, 51.50853
2  | Manchester | -2.25, 53.41667
3  | Glasgow    | -4.25, 55.86667
Run Code Online (Sandbox Code Playgroud)

coordinates列是点数据类型。我place使用以下方法将点插入表中:

st_geomfromtext('point($longitude $latitude)', 4326)
Run Code Online (Sandbox Code Playgroud)

请注意,我使用了 SRID。

给定任何坐标,我想找到离它最近的地方(按升序排列)。我目前提出的解决方案(通过阅读 MySQL 文档)如下所示:

select
    *,
    st_distance_sphere(`place`.`coordinates`, st_geomfromtext('Point($longitude $latitude)', 4326)) as distance
from place
order by distance asc;
Run Code Online (Sandbox Code Playgroud)

在查看了此处和其他地方的无数类似问题后,很明显这是一种鲜为人知(和较新的方式)的做事方式,因此没有太多内容,因此我要寻求一些澄清。

我的问题是:

  1. 这是最好的解决方案/我这样做对吗?
  2. 这种方法会利用我在coordinates列上的空间索引吗?
  3. 使用st_distance_sphere 时,是否需要指定地球的半径才能获得准确的结果?(编辑:不,它默认使用地球的半径)

编辑,这里是这些答案:

explain select ...; 返回:

id | select_type | table | partitions | …
Run Code Online (Sandbox Code Playgroud)

mysql geospatial latitude-longitude mysql-8.0

6
推荐指数
1
解决办法
1037
查看次数

标签 统计

geospatial ×1

latitude-longitude ×1

mysql ×1

mysql-8.0 ×1