简体   繁体   English

计算字段的优化排序

[英]Optimization sorting on calculated field

There are tables users and address .有表usersaddress The second table has column geo with type geometry .第二个表有geo类型为geometry During the searching users, current coords are sent on the server, and distance is calculated via such a sql request:在搜索用户的过程中,在服务器上发送当前坐标,通过这样一个 sql 请求计算距离:

SELECT 
   FLOOR(ST_Distance_Sphere(point(lng, lat), `geo`)) as distance 
FROM 
  address 
WHERE
  condition 

It works well, but also I need to sort by this field and it requires a lot of time.它运作良好,但我也需要按此字段排序,这需要很多时间。 Can someone advise how to optimize it?有人可以建议如何优化它吗?

MYSQL 5.7 MYSQL 5.7

Classic problem.经典问题。 No quick solution.没有快速的解决方案。 All solutions involve adding a "bounding box" to the WHERE , plus some index that makes use of that bounding box.所有解决方案都涉及向WHERE添加一个“边界框”,以及一些使用该边界框的索引。 Here's my 'dissertation' on the problem, plus detailed solutions: http://mysql.rjweb.org/doc.php/find_nearest_in_mysql这是我关于这个问题的“论文”,以及详细的解决方案: http://mysql.rjweb.org/doc.php/find_nearest_in_mysql

I am assuming hour "condition" includes我假设小时“条件”包括

WHERE distance < 100

or或者

ORDER BY distance LIMIT 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM