空间距离排序导致的异常

在使用MySQL 5.7.30时,遇到一个排序问题,SQL查询附近用户数据时,对distance排序导致无结果。排查发现是st_distance_sphere函数参数错误,location字段存在经度和纬度颠倒,导致超出有效范围。修正数据后,查询恢复正常。提醒:遇到SQL无结果且无错误提示时,可检查warnings信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL版本:5.7.30

简介:

        今天有同学反馈获取某个用户的附近的人数据时没有数据返回,这明显不正常,接下来为排查过程。

获取附近的人的语句如下:

SELECT

FLOOR(st_distance_sphere((select location from t_user_location where user_id = 1234), location)) distance,b.*

FROM t_family b

left join `t_family_location` a

on a.family_id = b.id

where st_distance_sphere((select location from t_user_location where user_id = 1234), location) < 81843200000

AND b.public=1

ORDER BY distance asc,b.id

排查过程如下:

1. 排除了没有数据的可能性;

2. 去掉order by,有结果返回;

3. 去掉order by distance,对其它字段进行排序,也有结果返回;

问题:

        为何这里对 order by distance 进行排序会导致没有结果返回呢?

查看warnings输出

> show warnings;

1210 Incorrect arguments to st_distance_sphere

查询错误代码,发现是 location字段有错误的数据。

查看官方文档:

MySQL :: MySQL 5.7 Reference Manual :: 12.17.12 Spatial Convenience Functions

The geometry arguments should consist of points that specify (longitude, latitude) coordinate values:

  • Longitude and latitude are the first and second coordinates of the point, respectively.

  • Both coordinates are in degrees.

  • Longitude values must be in the range (-180, 180]. Positive values are east of the prime meridian.

  • Latitude values must be in the range [-90, 90]. Positive values are north of the equator.

经度值必须在 (-180, 180]范围内,纬度值必须在[-90, 90]。

结果:

        检查发现,有一条数据经度和纬度搞反了,纬度值超出了范围[-90, 90],修改完该条数据后,再次执行该sql,这次有数据返回了,到此,问题解决。

小结:

        当发现sql执行结果没显异常,又没有报错的时候,记得执行一下show warnings,或许能发现问题所在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FightingFreedom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值