2016-12-21 5 views
1

私は約5000のエントリを持つ場所(緯度と経度)を含むデータベースを持っています。PHP対Mysqlの速度三角形分割

ここでは、特定の位置から50~75マイル離れた場所をすべて選択したいと考えています。

私の現在のSQLは次のとおりです。$coordinates['lat']$coordinates['lng']

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)))) 

は位置の緯度/経度います。

私の質問は次のとおりです。PHPでこのような計算を行うほうが速いのがよいでしょうか?

これは、すべてのエントリ(5000)をPHPにプルして距離を計算することを意味しますか?

ありがとうございました!

+1

MySQLはインデックスを使用できます。これは、通常、データセットをより管理しやすいサイズにカットする上で非常に貴重です。つまり、これは実際には小さなデータセットなので、多分大きな違いはありません! – Strawberry

+1

@Strawberryそのような複雑な式の結果を使用しているときはインデックスを使用できません。 – Barmar

+1

私は式を繰り返す代わりに 'BETWEEN 51 AND 74'を使用します。 – Barmar

答えて

0

WHERE句に「バウンディングボックス」とINDEX(latitude)を追加すると、部分的にインデックスを使用して、参照する行数を減らすことができます。 - ご出発の緯度 - と+ 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) `

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

$ aと$ bは比較的簡単です:

バウンディングボックスは次のようになります。

私は5000がほとんど心配する価値がないことに同意します。百万行があれば、私はthisにあなたをプッシュします。

関連する問題