问题描述
下面的 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 (将#修改为@)