I have a sql query that runs in like 2 seconds in mysql (Navicat) and it runs through 600,000 locations returning all the table data and calculates the distance between a lat and long that I put in.
The SQL is something like this:
select *, 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) -
radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
FROM places
GROUP BY places.user_id
HAVING distance > 0
ORDER BY distance LIMIT 0 , 10;
In the above query 37 and -122 are passed into the query as lat and long.
When I run this in Navicat, it's 2 seconds, but it takes minutes in Cakephp or straight php using mysql_query().
Why does this occur and how do I fix it?
I'm running both MySQL and Apache locally on a Win7 4GB box running XAMPP.