How to find nearest address by lat,long
<p>I have created a location table where lat,long and location are saved. My requirement is when I pass a lat,long , will get the nearest location within 10 miles from that location table. Location table structure is as follows:- </p>

<pre><code>CREATE TABLE IF NOT EXISTS `locations` (
  `latitude` float(14,12) unsigned NOT NULL,
  `longitude` float(14,12) unsigned NOT NULL,
  `location` varchar(150) NOT NULL,
  PRIMARY KEY (`latitude`,`longitude`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</code></pre>

<p>and 
My query is :-</p>

<pre><code>  SELECT 
    (3959 * ACOS(COS(RADIANS(lat)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS(long)) + SIN(RADIANS(lat)) * SIN(RADIANS(latitude)))) AS distance,
    location
FROM
    locations
HAVING distance &lt; 10
ORDER BY distance
LIMIT 0 , 1
</code></pre>

<p>I am using MySql 5.6. 
There are around 39 thousands data in location table.
When this query executes it is taking 5 to 6 second. </p>

<p>Can anybody help out how could I reduce the execution time?</p>

<p>Thanks!   </p>