我有一个查询,其中有一些子查询(内部选择),我正在尝试找出哪个对性能更好,一个更大的查询或许多较小的查询,我发现很难尝试并在变化时计算差异一直在我的服务器上。
我使用下面的查询一次返回 10 个结果以显示在我的网站上,使用分页(偏移量和限制)。
SELECT adverts.*, breed.breed, breed.type, sellers.profile_name, sellers.logo, users.user_level ,
round( sqrt( ( ( (adverts.latitude - '51.558430') * (adverts.latitude - '51.558430') ) * 69.1 * 69.1 ) + ( (adverts.longitude - '-0.0069345') * (adverts.longitude - '-0.0069345') * 53 * 53 ) ), 1 ) as distance,
( SELECT advert_images.image_name FROM advert_images WHERE advert_images.advert_id = adverts.advert_id AND advert_images.main = 1 LIMIT 1) as imagename,
( SELECT count(advert_images.advert_id) from advert_images WHERE advert_images.advert_id = adverts.advert_id ) AS num_photos
FROM adverts
LEFT JOIN breed ON adverts.breed_id = breed.breed_id
LEFT JOIN sellers ON (adverts.user_id = sellers.user_id)
LEFT JOIN users ON (adverts.user_id = users.user_id)
WHERE (adverts.status = 1) AND (adverts.approved = 1)
AND (adverts.latitude BETWEEN 51.2692837281 AND 51.8475762719) AND (adverts.longitude BETWEEN -0.472015213613 AND 0.458146213613)
having (distance <= '20')
ORDER BY distance ASC
LIMIT 0,10
从主查询中删除下面的 2 个内部选择,然后在我的 php 循环中,调用 2 个选择 10 次,循环中的每条记录一次,会更好吗?
( SELECT advert_images.image_name FROM advert_images WHERE advert_images.advert_id = adverts.advert_id AND advert_images.main = 1 LIMIT 1) as imagename,
( SELECT count(advert_images.advert_id) from advert_images WHERE advert_images.advert_id = adverts.advert_id ) AS num_photos