Effectively selecting the closest (distance) record from a database

前端 未结 4 1850
再見小時候
再見小時候 2020-12-16 03:53

I\'ve got a database with a 40k venues and growing right now.

Assuming I\'m the red dot

\"Easy\"
I

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 04:36

    Create a "bounding box" to use in a WHERE clause in your SQL query as described in this article on Movable Type (with PHP code examples), then include the Haversine formula in your query to calculate the actual distances, and order the result by distance ASC. The nearest venue will then be the first return in the result set.

    It's the bounding box that helps your performance, because it means you're only doing the expensive distance calculation on a small subset of your data

    If the initial query doesn't return any records, widen the bounding box, and execute the query again until you do get a response.

提交回复
热议问题