如何按距给定点的距离对行进行排序,mysql?
我试图从表中得到 100 分,并且到给定点的距离最近。
我正在使用
SELECT *, GLENGTH(
LINESTRINGFROMWKB(
LINESTRING(
ASBINARY(
POINTFROMTEXT("POINT(40.4495 -79.988)")
),
ASBINARY(pt)
)
)
)
AS `distance` FROM `ip_group_city` ORDER BY distance LIMIT 100
(是的,这很痛苦。我刚刚用谷歌搜索过。我不知道如何正确测量MySQL中的距离)
执行需要很长时间。 EXPLAIN
表示不存在 possible_keys
。
我在 pt
列上创建了一个 SPATIAL
索引:
CREATE SPATIAL INDEX sp_index ON ip_group_city (pt);
虽然我真的不知道如何正确使用它。你能帮我吗?
I'm trying to get 100 points from my table with a lowest distance to a given point.
I'm using
SELECT *, GLENGTH(
LINESTRINGFROMWKB(
LINESTRING(
ASBINARY(
POINTFROMTEXT("POINT(40.4495 -79.988)")
),
ASBINARY(pt)
)
)
)
AS `distance` FROM `ip_group_city` ORDER BY distance LIMIT 100
(Yeah, that's painful. I've just googled it. I have no idea how to measure distance in MySQL correctly)
It takes very long time for execute. EXPLAIN
says that there are no possible_keys
.
I created a SPATIAL
index on the pt
column:
CREATE SPATIAL INDEX sp_index ON ip_group_city (pt);
Though I don't really know how to use it correctly. Can you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您没有 WHERE 子句,因此没有受影响的索引。我认为您应该通过添加使用 MBR_(MySQL 5.0 或更高版本)或 ST_ 函数(MySQL 5.6 或更高版本)来改进此查询。
像这样的东西:
Because you don't have WHERE clause therefore no affected index. I think you should improve this query by add using MBR_ (MySQL 5.0 or later) or ST_ functions (MySQL 5.6 or later).
Something like:
我过去曾使用大圆方程来进行这些类型的计算。我不确定性能比较如何,但可能值得尝试和比较。
这里是一篇很好的SO帖子,介绍了如何在MySQL中执行此操作。
I've used the great circle equation to do these types of calculations in the past. I'm not sure how the performance compares but it might be worth trying it and comparing.
Here is a good SO post that goes over how to do it in MySQL.
查看这些问题:
Have a look at these questions: