PHP与数学三角测量中的Mysql速度

时间:2016-12-21 23:22:37

标签: php mysql performance

我有一个数据库,其中包含大约5000个条目的位置(纬度和经度)。

现在我想选择距离给定位置50-75英里之间的每个位置。

我目前的sql是:

SELECT a.ID AS ID, a.post_name, a.post_title 
FROM wp_posts a, wp_locations b 
WHERE b.post_id = a.ID 
AND 
    (3959 * ACOS( COS( RADIANS('{$coordinates['lat']}') ) * COS( RADIANS( b.map_coords_1 ) ) * COS( RADIANS( b.map_coords_2 ) - RADIANS('{$coordinates['lng']}') ) + SIN( RADIANS(  '{$coordinates['lat']}' ) ) * SIN( RADIANS( b.map_coords_1 ) ) ) ) < 75 
AND 
    ( 3959 * ACOS( COS( RADIANS('{$coordinates['lat']}') ) * COS( RADIANS( b.map_coords_1 ) ) * COS( RADIANS( b.map_coords_2 ) - RADIANS('{$coordinates['lng']}') ) + SIN( RADIANS(  '{$coordinates['lat']}' ) ) * SIN( RADIANS( b.map_coords_1 ) ) ) ) > 50
ORDER BY 
    ( 3959 * ACOS( COS( RADIANS(  '{$coordinates['lat']}' ) ) * COS( RADIANS( b.map_coords_1 ) ) * COS( RADIANS( b.map_coords_2 ) - RADIANS(  '{$coordinates['lng']}' ) ) + SIN( RADIANS(  '{$coordinates['lat']}' ) ) * SIN( RADIANS( b.map_coords_1 ) ) ) )

$coordinates['lat']$coordinates['lng']是该职位的纬度/经度。

现在我的问题是:在PHP中进行此类计算会更快/更好吗?

这意味着将所有条目(5000)拉入PHP,然后计算距离?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果添加到WHERE子句&#34;边界框&#34;和INDEX(latitude),则可以部分地使用索引,从而减少要查看的行数

边界框看起来像这样:

AND latitude  BETWEEN $a AND $b
AND longitude BETWEEN $c AND $d

$ a和$ b相对简单 - 你的起始纬度 - 和+ 75 * $miles_to_degrees (75 miles converted to latitude degrees). It is not practical to try to say anything about "more than 50". For longitude ($c and $d), you need 75 * $ miles_to_degrees / COS($ starting_latitude)`。

我同意5000几乎不值得担心。如果你有一百万行,我会把你推到this

相关问题