优化sql脚本

时间:2018-03-23 04:59:50

标签: mysql sql

大家好,我正在寻找优化此SQL脚本以获得更好性能的方法,任何人都可以帮助

With accounts As 
(
    select account_id, creation_date 
    from account 
    where program_distributor = 'brinks'
    and channel = 'online'
    and creation_year = 2017
),
Form_opens as 
(
    select session_id, log_time 
    from web_action_log 
    where web_action = 'open_dd_form'
),
Mapping as 
(
    select session_id, account_id 
    from web_link
)
Select
    trunc (acc.creation_date), 
    count(distinct acc.account_id), 
    count (distinct fo.account_id) 
from accounts acc 
left outer join mapping mp
    on acc.account_id = mp.account_id
Left outer join form_opens fo
    on mo.session_id = fo.session_id and 
    acc.creation_date > do.log_time
Group by trunc(acc.creation_date)
Order by 1;

1 个答案:

答案 0 :(得分:0)

尝试以下查询。您可以跳过写入子句,因为您没有多次查询子句输出表。在连接期间添加过滤条件也可以提高性能

select
    trunc (acc.creation_date), 
    count(distinct acc.account_id), 
    count (distinct fo.account_id) 
from account acc 
left outer join web_link mp
    on acc.account_id = mp.account_id
left outer join web_action_log fo
    on mo.session_id = fo.session_id 
    and acc.creation_date > fo.log_time 
    and fo.web_action = 'open_dd_form'
where   acc.program_distributor = 'brinks'
    and acc.channel = 'online'
    and acc.creation_year = 2017
group by trunc(acc.creation_date)
order by 1;