Hi,
I have an articles table and a categories table. I want to fetch 7 articles for each category. Currently I have this but it's terrible slow on large tables so it's not really a solution:
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
Using explain, it shows me it's doing a join type ALL & REF. The select types are PRIMARY and DEPENDENT SUBQUERY .
Is there a better solution?