简体   繁体   中英

Optimization sorting on calculated field

There are tables users and address . The second table has column geo with type geometry . During the searching users, current coords are sent on the server, and distance is calculated via such a sql request:

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

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. Here's my 'dissertation' on the problem, plus detailed solutions: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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