I have the 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
This takes a lot of time (about 0.119415 in my case). How to make it faster?
I see only one way: Perform an additional request for the comment number for each line from my main request. But it will be like executing queries in a loop. Sort of:
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 tips?
source share