微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL 查询性能调优 - Athena

如何解决SQL 查询性能调优 - Athena

下面的 sql 查询运行了将近 3 分钟。 3张表(C_table、E_table、V_table)都是外部表,根据C_id、dt进行分区。每个 CTE 返回大约 9000 列,每个表中大约有 4000 万条记录。有什么办法可以优化这个查询吗?

我试着按顺序改变顺序,但还是一样。

WITH S AS 
    (SELECT S.C_id AS C_id,S.dt AS date,S.CR_id AS CR_id,sum(AI) AS AI,sum(UE) AS UE,sum(UW) AS UW,cast(sum(VI) AS double) / cast(sum(AI) AS double) AS VR
    FROM "db_name"."C_table" S
    WHERE S.C_id IN (10422,10421,10420,10418,10417,10416,10415,10414)
    AND S.dt
        BETWEEN date '2021-01-1'
            AND date '2021-03-29'
    GROUP BY  S.C_id,S.dt,S.CR_id ),E AS 
    (SELECT E.C_id AS C_id,E.dt AS date,E.CR_id AS CR_id,sum(UIEO) AS UIEO
    FROM "db_name"."E_table" E
    WHERE E.C_id IN (10422,10414)
    AND E.dt
        BETWEEN date '2021-01-1'
            AND date '2021-03-29'
    GROUP BY  E.C_id,E.dt,E.CR_id ),V AS 
    (SELECT V.C_id AS C_id,V.dt AS date,V.CR_id AS CR_id,cast(sum(V_cmplt) AS double) / cast(sum(V_strt) AS double) AS VCR
    FROM "db_name"."V_table" V
    WHERE V.C_id IN (10422,10414)
    AND V.dt
        BETWEEN date '2021-01-1'
            AND date '2021-03-29'
    GROUP BY  V.C_id,V.dt,V.CR_id )
    
    
SELECT cp.name AS C_name,ct.name AS CR_name,S.date,S.AI,((cast(S.UE AS double) + cast(E.UIEO AS double)) / cast(S.UW AS double)) AS ER,V.VCR,S.VR
FROM S
JOIN V
    ON V.C_id = S.C_id
    AND V.date = S.date
    AND V.CR_id = S.CR_id
JOIN E
    ON E.C_id = S.C_id
    AND E.date = S.date
    AND E.CR_id = S.CR_id
JOIN "db_name"."C_lookup" cp
    ON cp.id = S.C_id
JOIN "db_name"."CR_lookup" ct
    ON ct.id = S.CR_id

感谢任何帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。