我有以下查询:
Select diary_id,
(select count(*)
from `comments` as c
where c.d_id = d.diary_id) as diary_comments
From `diaries` as d
执行时间很长(在我的情况下接近0.119415秒)。如何使它变得更快?
我只看到一种方法:为主查询中的每一行执行附加的评论数量查询。但这将类似于循环中执行查询。大概像这样:
while ($r = mysql_fetch_array($res))
{
$comments = mysql_query("select count(*) from `comments` where d_id = ".$r['diary_id']);
}
我认为这是一个不好的策略。还有其他建议吗?