I'm trying to optimize the following query which is very very slow if I add the group by and the having condition in the end. Is there a different way to process it?
set @lat = (select latitude from t_zip where code in ( SELECT id_house FROM hp0.t_house_config where id_house=39));
set @lon = (select latitude from t_zip where code in ( SELECT id_house FROM hp0.t_house_config where id_house=39));
SELECT sp2.rkd_property_zip,
sp1.code,
round( (6371.393 * ACOS(COS(RADIANS(@lat)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS(@lon)) + SIN(RADIANS(@lat)) * SIN(RADIANS(latitude)))),0) as distance
FROM
hp0.t_zip sp1
inner join t_rdk sp2 on sp1.code = sp2.rkd_property_zip
group by distance
having distance<10