【发布时间】:2021-04-15 11:42:21
【问题描述】:
连接三个表(产品、描述、图像)的以下选择查询需要 9 秒才能返回 10 行!
SELECT `products`.id, `products`.serialNumber, `products`.title, `descriptions`.description1, `descriptions`.description2, `descriptions`.description3, `descriptions`.description4,`products`.price,`products`.colors, `products`.category, `products`.available, `products`.status, GROUP_CONCAT(DISTINCT `images`.file_name ORDER BY `images`.id) AS images
FROM `products`
INNER JOIN `descriptions`
ON `products`.id = `descriptions`.product_id
LEFT JOIN `images`
ON `products`.id = `images`.product_id
WHERE `products`.status = 1
GROUP BY `products`.id
ORDER BY `products`.id DESC
LIMIT 10;
所以经过一些研究,我得到了下面的答案,其中包含连接两个表并且只需一秒钟即可返回 10 行,我如何添加第三个表(描述)?谢谢
SELECT *,
( SELECT group_concat(`images`.file_name)
FROM `images`
) AS images
FROM `products`
JOIN `images` ON `products`.id = `images`.product_id
WHERE `products`.status = 1
GROUP BY `products`.id
ORDER BY `products`.id DESC
LIMIT 10
【问题讨论】:
-
发布性能问题时,您应该提供表格定义、数量和解释计划。在大多数情况下,性能不佳是由于索引不佳(或没有索引),因此需要表定义和解释计划。时间对于卷来说可能是合理的,顺便说一句,限制是对结果集的限制而不是读取限制..
-
是的,但是经过一些研究,我发现我的查询中的问题..为此我只发布了查询...正确的答案确实将执行时间从 8 秒减少到 1第二个!
标签: mysql sql json join left-join