我需要重新优化这个查询,它很慢。
SELECT al.alias_title, al.title, al.hits, al.created_on,
u.first_name, u.surname,
uf.real_name, uf.user_name,
T.guid_id
FROM album al
INNER JOIN (SELECT imx.id, aix.album_id, imx.guid_id
FROM image imx
INNER JOIN album_image aix
ON imx.id = aix.image_id
ORDER BY FLOOR(RAND() * (SELECT COUNT(1)
FROM image))) AS T
ON al.id = T.album_id
LEFT JOIN user u
ON al.user_id = al.id
LEFT JOIN user_flickr uf
ON u.id = uf.user_id
WHERE al.approved ='Yes'
AND al.visible ='1'
GROUP BY T.album_id
ORDER BY al.title;
我需要一个随机图像来每次显示每个画廊。存在多对多关系——图像、专辑和专辑图像。我需要每个专辑的随机图像,然后按标题排序专辑。我认为内部查询和 rand 函数非常慢。我查看了临时表,但不确定这是否是最好的方法。