Mysql递归查询/余弦球面定律
我想从坐标(纬度/经度)搜索最近的位置到mysql数据库中,我使用余弦球面定律来搜索这些地方:
SELECT onlycoord.id, locations.name, locations.ascii,
locations.latitude, locations.longitude,
locations.country, locations.elevation,
locations.gtopo30, locations.timezone,
(6371 * ACOS(
COS( RADIANS( 48.48 ) ) *
COS( RADIANS( onlycoord.latitude ) ) *
COS(
RADIANS( onlycoord.longitude ) -
RADIANS( 2.20 )
) +
SIN( RADIANS( 48.48 ) ) *
SIN( RADIANS( onlycoord.latitude ) )
)) AS distance
FROM onlycoord USE INDEX (coordinate)
LEFT JOIN locations USE INDEX (id)
ON (onlycoord.id = locations.id)
WHERE onlycoord.latitude BETWEEN (
48.48 - ( 5 / 111 )
) AND (
48.48 + ( 5 / 111 )
) AND onlycoord.longitude BETWEEN (
2.20 - ( 5 / ABS( COS(
RADIANS( 48.48 )
) * 111 ) )
) AND (
2.20 + ( 5 / ABS( COS(
RADIANS( 48.48 )
) * 111 ) )
)
ORDER BY distance ASC
LIMIT 0, 20
其中6371是地球半径(公里),111(公里)是1°纬度,cos(纬度) ) * 111(公里)是 1° 经度,5(公里)是搜索半径。
问题:我希望至少找到 8 个城市,但 5 公里半径很小,但对于密集区域(许多城市)来说速度很快,因此如果我使用对于密集区域太大的搜索半径
,查询会很慢(许多结果:排序依据),但对于非压缩搜索半径太小,无法找到至少 8 个城市...
我怎样才能进行自动增加搜索的递归查询半径 (x2) 如果找到的城市数 < 8(仅使用mysql)?
谢谢
I want to search nearest locations from coordinates (lat/lng) into mysql database, I use spherical law of cosines to searching these places :
SELECT onlycoord.id, locations.name, locations.ascii,
locations.latitude, locations.longitude,
locations.country, locations.elevation,
locations.gtopo30, locations.timezone,
(6371 * ACOS(
COS( RADIANS( 48.48 ) ) *
COS( RADIANS( onlycoord.latitude ) ) *
COS(
RADIANS( onlycoord.longitude ) -
RADIANS( 2.20 )
) +
SIN( RADIANS( 48.48 ) ) *
SIN( RADIANS( onlycoord.latitude ) )
)) AS distance
FROM onlycoord USE INDEX (coordinate)
LEFT JOIN locations USE INDEX (id)
ON (onlycoord.id = locations.id)
WHERE onlycoord.latitude BETWEEN (
48.48 - ( 5 / 111 )
) AND (
48.48 + ( 5 / 111 )
) AND onlycoord.longitude BETWEEN (
2.20 - ( 5 / ABS( COS(
RADIANS( 48.48 )
) * 111 ) )
) AND (
2.20 + ( 5 / ABS( COS(
RADIANS( 48.48 )
) * 111 ) )
)
ORDER BY distance ASC
LIMIT 0, 20
Where 6371 is earth radius (km), 111 (km) is 1° of latitude, cos(latitude) * 111 (km) is 1° of longitude and 5 (km) is search radius.
Problem : I want a minimum of 8 cities found, but 5 kms radius is small but fast for condensed zone (many cities), so if I use a search radius
too big for condensed zone, query is slow (many results : order by), but for non-condensed the search radius
is too small to find at least 8 cities...
How can I make a recursive query which increases automatically the search radius
(x2) if the count of found cities < 8 (using mysql only) ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL 不支持递归查询。
您必须根据上一个查询的结果进行新的查询。
顺便说一句,对于空间查询,我建议使用空间数据库。
MySQL doesn't support recursive queries.
You have to make new queries depending on the result of the previous query.
By the way, for spatial queries I'd recommend a spatial database.