I have following query:
Select diary_id,
(select count(*)
from `comments` as c
where c.d_id = d.diary_id) as diary_comments
From `diaries` as d
It takes long time (near 0.119415 in my case). How to make it faster?
I see only one way: Doing additional query for comment number for each row from my main query. But it will be something like doing queries in cycle. Something like:
while ($r = mysql_fetch_array($res))
{
$comments = mysql_query("select count(*) from `comments` where d_id = ".$r['diary_id']);
}
I think this is a bad strategy. Any other advice?