聚合内部查询SQL优化

时间:2020-01-27 15:27:20

标签: sql postgresql metabase

我有3张桌子:

create table users
    (
        user_id varchar(50),
        birth_year int,
        country varchar(50)
    )


create table notifications 
    (
        status varchar(50), 
        user_id varchar(50), 
        created_date datetime
    )

create table transactions
    (
        transaction_id varchar(50),
        user_id varchar(50),
        created_date datetime
    )

我要做的是让所有收到通知的用户拥有通知,通知到达前7天与通知之后7天的平均交易量有何不同 按国家和年龄段到达。

我的工作如下:

select q.country
, case when q.age <= 18 then '<= 18'
    when q.age <= 30 then '19 - 30'
    when q.age <= 45 then '31 - 45'
    when q.age <= 60 then '46 - 60'
    else '> 60' end as age_group
, AVG(q.prev_transactions*1.0) as avg_prev_transactions, AVG(q.post_transactions*1.0) as avg_post_transactions
from (
    select n.user_id, n.created_date, u.country, (2019 - u.birth_year) as age
    , count(distinct prev.transaction_id) as prev_transactions, count(distinct post.transaction_id) as post_transactions
    from notifications n
    left outer join transactions post on n.user_id = post.user_id and post.created_date > n.created_date and post.created_date < n.created_date + interval '7' day
    left outer join transactions prev on n.user_id = prev.user_id and prev.created_date < n.created_date and prev.created_date > n.created_date - interval '7' day
    left outer join users u on u.user_id = n.user_id
    where status = 'SENT'
    group by n.user_id, n.created_date, u.country, (2019 - u.birth_year)
    --order by n.user_id asc, n.created_date asc
    ) as q
group by q.country, case when q.age <= 18 then '<= 18'
    when q.age <= 30 then '19 - 30'
    when q.age <= 45 then '31 - 45'
    when q.age <= 60 then '46 - 60'
    else '> 60' end

我想知道是否有办法提高效率。

谢谢

1 个答案:

答案 0 :(得分:0)

您在“交易”上的两个左连接可能是一个问题。如果有30个prev事务和30个post事务,则这两个联接实质上是笛卡尔联接在一起,从而创建900个pre-post配对。然后使用DISTINCT将其减少到30。但是您正在做的工作是创建然后删除无聊的行。

您可以将它们放在每个子选择中,而不是作为联接。

select n.user_id, n.created_date, u.country, (2019 - u.birth_year) as age,
    (select count(*) from transactions post on n.user_id = post.user_id and post.created_date > n.created_date and post.created_date < n.created_date + interval '7' day) as post_transactions,
...

此外,为什么左派反对用户?对于没有用户的通知,可以获得什么可能有意义的输出?

相关问题