在 table 中出现 id 并在另一个 table 中出现所有元素的最佳方法

Best approach to ocurrences of ids on a table and all elements in another table

好吧,我需要的查询很简单,也许在另一个问题中,但是我需要的是性能方面的东西,所以:

我有 table 个用户,有 10.000 行,table 包含 ID、电子邮件和更多数据。

在另一个名为 orders 的 table 中,我有更多的行,可能有 150.000 行。

在这个 orders 中,我有下订单的用户的 id,还有 status的顺序。 status 可以是 0 到 9 之间的数字(或 null)。

我的最终要求是让每个用户都具有 id、电子邮件、其他一些列,以及状态为 3 或 7 的订单数。 它不关心它的 3 或 7,我只需要数量

但我需要以低影响的方式(或高效的方式)执行此查询。

最好的方法是什么?

我需要 运行 用 postgres 10 重新编辑这个。

这听起来像 joingroup by:

select u.*, count(*)
from users u join
     orders o
     on o.user_id = u.user_id
where o.status in (3, 7)
group by u.user_id;

Postgres 通常非常擅长优化这些查询——上面假设 users(user_id) 是主键——所以这应该工作得很好。