我的论坛的索引页面如下所示:
| Forum | Topics | Answers |
----------------------------
| Forum A | 123 | 45678 |
| Forum B | 345 | 23128 |
| Forum C | 567 | 2328 |
这是我到目前为止有效的 SQL 代码,但我认为必须有更好的解决方案:
SELECT f.`id`, f.`name`, f.`description`, f.`type`,
(SELECT COUNT(`id`)
FROM threads
WHERE `forum_id` = f.`id`) AS num_threads,
(SELECT COUNT(p.`id`)
FROM threads t, posts p
WHERE p.thread_id = t.id
AND t.forum_id = f.id) AS num_posts
FROM `forums` f ORDER BY `position`
你会如何加快这个查询?子查询的任何替代方案?
提前致谢!