考虑这张表 ( comments
):
id | post_id | text
------------+---------|----------------
79507 | 12 | Lorem Ipsum
79544 | 12 | Foo, bar
79545 | 14 | Interesting...
这个聚合查询:
SELECT comment_id, SUM(vote) AS votes
FROM votes
GROUP BY comment_id;
comment_id | votes
------------+-------
79507 | 3
79544 | 4
79545 | 1
我正在寻找加入comments
表和聚合查询,但只对数据的一个非常小的子集(只有一个特定的post_id
)感兴趣。这种天真的方法使用子查询来正确返回post_id
12 的结果:
SELECT comment_id, votes, text FROM comments c LEFT JOIN
(SELECT comment_id, SUM(votes) AS vote
FROM votes
GROUP BY comment_id) AS v
ON c.id = v.comment_id
WHERE c.post_id = 12;
comment_id | votes | text
------------+-------|----------------
79507 | 3 | Lorem Ipsum
79544 | 4 | Foo, bar
然而,这是非常低效的,因为我们正在计算整个表的内部子查询,但我们只对它的一个非常小的子集感兴趣(votes
这个应用程序中的表很大)。
Intuitively, it seems we should be filtering the inner query and there we're missing a WHERE comment_id IN (...)
in the subselect. However, we don't know which comment_id
s we will need at that stage in the computation. Another subselect inside the subselect could be used to retrieve the appropriate comment_id
s, but that seems very clumsy.
I'm inexperienced in SQL and not sure if there exists a cleaner solution. Perhaps the subselect approach is the wrong one altogether.