This question already has answers here:
Database: Best performance way to query geo location data?
                                
                                    (11个答案)
                                
                        
                        
                            How to optimize SQL query with calculating distance by longitude and latitude?
                                
                                    (2个答案)
                                
                        
                                6年前关闭。
            
                    
我的桌子上有超过800万条记录(geonames)。该表内有19列,其中2列是经度和纬度。

我希望从纬度和经度值中找到最近的地方,然后执行以下查询:

SELECT * , ( 6371 * ACOS( COS( RADIANS( 40.8333333 ) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS( 14.25 ) ) + SIN( RADIANS( 40.8333333 ) ) * SIN( RADIANS( latitude ) ) ) ) AS distance
FROM geoname
WHERE fclass =  'P'
HAVING distance <25
ORDER BY distance
LIMIT 0 , 20


我设置了ftree,纬度和经度的btree索引。

问题是查询需要5.6027秒。太多了。
有没有优化的方法?我做错了吗?

谢谢

最佳答案

就我所知,常规关系数据库并不意味着提供空间近似查询的功能。

在您的位置,我要么将数据移至空间数据库,要么将数据插入度量树(此处可能是kd树),然后针对该树发出查询。

10-04 23:36
查看更多