Mysql递归查询/余弦球面定律

发布于 2024-10-10 00:38:32 字数 1267 浏览 9 评论 0原文

我想从坐标(纬度/经度)搜索最近的位置到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2024-10-17 00:38:32

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文