I have sql query where need to join 5 tables. I have tried this query so far. It is working but taking long time. what can be done here to optimise following query?
$select_query = 'select ';
$select_query .= "ROUND((
6371 * ACOS(
COS(RADIANS('.$lat.')) COS(RADIANS(lat)) COS(
RADIANS(lng) - RADIANS('.$lng.')
) + SIN(RADIANS('.$lat.')) * SIN(RADIANS(lat))
)
),2) AS property_distance ,";
$select_query .= "
pro.id as id,
pro.user_id,
pro.category_id,
pro.total_price,
pro.size,
pro.lat,
pro.lng,
pro.city,
pro.city_english,
pro.created_at,
pimg.image as property_images,
pimg.property_id,
pa.property_id,
pa.category_attribute_id,
pa.is_multiple_data,
pa.attribute_value,
ca.category_id,
ca.attribute_name,
ct.category_id,
ct.category_name,
cat.attribute_id,
cat.attribute_label,
cat.locale
FROM
property pro FORCE INDEX (property_index)
left join property_images pimg on pro.id=pimg.property_id
JOIN property_attributes pa ON
pa.property_id = pro.id
left JOIN category_attributes ca ON
ca.id = pa.category_attribute_id
left JOIN category_attributes_translations cat ON
ca.id = cat.attribute_id
left JOIN categories_translation ct ON
pro.category_id = ct.category_id
WHERE pro.is_confirm='1' and pro.status='1' and pro.deal_finish='0' and cat.locale='" . $locale . "' and ct.locale='" . $locale . "'
GROUP BY pro.id HAVING property_distance<=10 ORDER by pro.id DESC";
Then finally running this query.
Please suggest me the proper way to optimise this query.