以下查询会在上传到我的外部服务器时对页面加载产生负面影响:
SELECT
compositions.composer, compositions.composition, compositions.date, compositions.performedBy
FROM
compositions
INNER JOIN
(SELECT composer
FROM compositions
WHERE
composer IN (
SELECT composer FROM compositions
GROUP BY composer
HAVING COUNT(1) < 15
)
ORDER BY RAND()
LIMIT 1) AS RandComposer
ON
compositions.composer = RandComposer.composer
ORDER BY compositions.composition
总页面加载时间超过4秒,其中大部分是由于包含查询的PHP页面的等待时间(see Pingdom screenshot)。
表格中的所有相关字段&#39;组合&#39;被编入索引(see phpMyAdmin screenshot)。
我也可以在phpMyAdmin中提供EXPLAIN的输出,但我没有足够的声誉来发布第三个链接。
如果有人可以查看查询并告知如何改进查询以减少页面加载时间,我将非常感激。
答案 0 :(得分:0)
重新构建查询解决了问题。以下代码会立即加载。
SELECT
compositions.composer, compositions.composition,
compositions.date, compositions.performedBy
FROM
compositions
INNER JOIN
(
SELECT composer FROM compositions
GROUP BY composer
HAVING COUNT(*) < 15
ORDER BY RAND()
LIMIT 1
) AS RandComposer
ON
compositions.composer = RandComposer.composer
ORDER BY
compositions.composition;