我在Symfony2网站上使用了以下查询,用于将最近的纬度和经度对定位到地图上的给定点。有一张桌子' latlong'它存储了这些对,以及一个表'fos_user_user'它存储用户和“用户名”。存储有关这些用户的数据的表(这是我们从wordpress移动所有数据时的宿醉)。问题是它运行得非常慢 - 当我们在旧的WordPress网站上运行非常类似的查询时,它过去相当快。有没有人对如何优化它有任何想法?
SELECT
latlong.user AS id,
fos_user_user.username AS username,
latlong.lat AS lat,
latlong.lng AS lng,
fos_user_user.email_canonical AS email,
firstname.VALUE AS firstname,
lastname.VALUE AS lastname,
address1.VALUE AS address1,
address2.VALUE AS address2,
address3.VALUE AS address3,
postcode.VALUE AS postcode,
image.VALUE AS image,
(
3959 * ACOS(
COS(
RADIANS('51.4424728')
) * COS(
RADIANS(lat)
) * COS(
RADIANS(lng) - RADIANS('7.2586558')
) + SIN(
RADIANS('51.4424728')
) * SIN(
RADIANS(lat)
)
)
) AS distance
FROM
latlong
JOIN fos_user_user ON latlong.user = fos_user_user.id
LEFT JOIN usermeta AS firstname ON firstname.user_id = latlong.user
AND firstname.field_id = '15'
LEFT JOIN usermeta AS lastname ON lastname.user_id = latlong.user
AND lastname.field_id = '16'
LEFT JOIN usermeta AS image ON image.user_id = latlong.user
AND image.field_id = '11'
LEFT JOIN usermeta AS address1 ON address1.user_id = latlong.user
AND address1.field_id = '2'
LEFT JOIN usermeta AS address2 ON address1.user_id = latlong.user
AND address1.field_id = '3'
LEFT JOIN usermeta AS address3 ON address1.user_id = latlong.user
AND address1.field_id = '4'
LEFT JOIN usermeta AS postcode ON postcode.user_id = latlong.user
AND postcode.field_id = '5'
LEFT JOIN usermeta AS public ON public.user_id = latlong.user
AND public.field_id = '10'
WHERE
public.value = 'YES'
HAVING
distance < 25
ORDER BY
distance ASC
LIMIT
0, 5