我正在嘗試查找自上次創建用戶以來的三個月內創建的用戶數量。所有按國家分組。性能聚合where子句
這裏是一個可行的查詢:
select count(u.id) as numberOfUsers,
s.state
from users u
join states s on u.state_id = s.id
where u.creationdate > (
select max(u2.creationdate)
from users u2
where u2.state_id = s.id
) - interval '3 months'
group by s.state
但是,它需要100秒。有人能給我一個更高性能的嗎?
我希望這工作:
select count(u.id) as numberOfUsers,
s.state, max(u2.creationdate) as lastCreated
from users u
join states s on u.state_id = s.id
where u.creationdate > lastCreated - interval '3 months'
group by s.state
太棒了!我修改了一下你的查詢並獲得了82ms。 (vs 100000ms,這只是幾個數量級) – 2011-03-08 22:53:59