I have need find nearby postcode of point in google map , I am using SQL – Haversine formula. below is my query
$query = $this->db->query("SELECT Postcode,latitude,longtitude, ACOS(COS(RADIANS(latitude)) *
COS(RADIANS(longtitude)) * COS(RADIANS($latitude)) * COS(RADIANS($longtitude)) +
COS(RADIANS(latitude)) * SIN(RADIANS(longtitude)) * COS(RADIANS($latitude)) *
SIN(RADIANS($longtitude)) + SIN(RADIANS(latitude)) * SIN(RADIANS($latitude))) *
3963.1 AS Distance
FROM postalscheme
HAVING Distance <= 4 ");
Data is fetched correctly using this query, but my problem is its taking long time to fetch data [3 or 5 min for fetch 20 thousand to 60 thousand data ] .
please help me to optimize this query and work fast .