【发布时间】:2014-04-02 18:15:19
【问题描述】:
我的距离计算脚本存在严重的性能问题。
我在一个数据库中有大约 3000 个位置(这最终会增加一倍)。数据库结构相当复杂(类别、子类别)但带有 time();我看到这些查询并没有花费太多时间。
我有一个用户的纬度和经度 $_GET,我使用这个计算来确定该位置是否在某个半径范围内:
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
// some sql queries to get the lat/lon from the locations
if ((distance($_GET["lat"], $_GET["long"], $row3["content"], $row4["content"], "K") . "") < 10) {
//push to multidimensional array
}
$row3["content"] 和 $row4["content"] 是纬度和经度值。对于 3000 个位置,此计算最多需要 13 秒!
我读到这个: Fastest Way to Find Distance Between Two Lat/Long Points
我认为基于纬度和经度的 $_GET 绘制一个框的选项可能已经删除了当前的计算。在 sql 查询中,我已经可以过滤掉 10 公里范围之外的位置。
但我有两个问题:
- 如果我将 SQL 更改为以下内容:... WHERE LAT >= x1 AND
- 作者在解释中谈到了“单位”。我一直在玩 lat/lon 值,但我如何实际计算 x1、x2、y1、y2,其中 $_GET 值是距离为 10 公里的中心点?
谢谢。
【问题讨论】:
-
如果您对性能很认真,您应该考虑一些 GIS/空间解决方案。
-
我确定问题与您的数据库代码有关,而不是计算两个坐标之间的距离。我不知道如何帮助您解决您的问题,因为您没有向我们展示这些问题。
-
要显示的代码很多,但基本上是这样的: select * from table A - while mysqli_fetch_assoc select reference id's from table B (match with id's from table A) - while mysqli_fetch_assoc select LAT from table C(与表 B 中的参考 id 匹配)-而 mysqli_fetch_assoc 从表 C 中选择 LON(与表 B 中的参考 id 匹配)-而 mysqli_fetch_assoc { //距离计算 }
-
这就是为什么大多数人会为他们的 SQL 查询生成一个边界框,以减少他们需要计算距离的记录数量:它并没有消除计算距离的需要,因为你是通过计算进一步减少数字,但它减少了您需要执行计算的次数
-
谢谢,我已经更新了我的答案。我确实发现这是一种非常有效的方法,我能够将加载时间从 13 秒减少到只有 1 秒,这仅仅是因为在 SQl 查询中使用了边界框。
标签: php mysql latitude-longitude