我有一个文章表和一个类别表。我想为每个类别获取 7 篇文章。目前我有这个,但在大桌子上速度很慢,所以它不是一个真正的解决方案:
SELECT id,
title,
categories_id,
body,
DATE_FORMAT(pubdate, "%d/%m/%y %H:%i") as pubdate
FROM articles AS t
WHERE (
SELECT COUNT(*)
FROM articles
WHERE t.categories_id = categories_id
AND id< t.id AND publish = 1
AND expires > '2008-12-14 18:38:02'
AND pubdate <= '2008-12-14 18:38:02'
) < 7
ORDER BY categories_id DESC
使用解释,它向我显示它正在执行连接类型 ALL 和 REF。选择类型是 PRIMARY 和 DEPENDENT SUBQUERY 。
有更好的解决方案吗?