mysql按照距离查询,MYSQL查询性能-按距离搜索

I have the following MYSQL query which is running on a table with around 50,000 records. The query is returning records within a 20 mile radius and i'm using a bounding box in the where clause to narrow down the records. The query is sorted by distance and limited to 10 records as it will be used on a paginated page.

The query is currently taking 0.0210 seconds to complete on average, but because the website is so busy I am looking for ways to improve this.

The adverts table has around 20 columns in it and has an index on the longitude and latitude columns.

Can anyone see anyway to improve the performance of this query? I was thinking about creating a separate table which just has the advert_id and longitude and latitude fields, but was wondering if anyone had any other suggestions or ways to improve the query below?

SELECT adverts.advert_id,

round( sqrt( ( ( (adverts.latitude - '52.536320') *

(adverts.latitude - '52.536320') ) * 69.1 * 69.1 ) +

( (adverts.longitude - '-2.063380') *

(adverts. longitude - '-2.063380') * 53 * 53 ) ),

1 ) as distance FROM adverts

WHERE (adverts.latitude BETWEEN 52.2471737281 AND 52.8254662719)

AND (adverts.longitude BETWEEN -2.53875093307 AND -1.58800906693)

having (distance <= 20)

ORDER BY distance ASC

LIMIT 10

解决方案

You have to use spatial data formats and spatial indexes: how to use them.

In particular, you have to use the POINT data format to store both latitude and longitude in a single column, then you add a spatial index to that column.

The spatial index is usually implemented as an R-tree (or derivations) so that the cost of searching all points in a given area is logarithmic.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值