0

我有以下查询,它在 XS/S 上运行的时间太长,并且出现错误,错误代码建议将 WH 升级到更高的尺寸。我们做到了(M),它在 10 分钟内运行。有什么建议可以微调这个查询吗?请注意,这只是实际查询的几行。实际查询对于其他列有 20 多个重复行(对于 windows 函数 - first_value(CAT) over (partition by DATE_VAL, CAM_ID, GRP_ID order by CAT nulls last) as CATEGORY)。基于查询配置文件,它花费了 89% 的精力来评估/处理 windows 函数代码。

select * from (
        select 
                DATE_VAL, 
                CAM_ID, 
                GRP_ID,                 
                first_value(CAT) over (partition by DATE_VAL, CAM_ID, GRP_ID order by CAT nulls last) as CATEGORY,                 
                ROW_NUMBER() OVER ( partition by DATE_VAL, CAM_ID, GRP_ID order by TRCK nulls last ) as rnk
                from (
                select distinct
                    REPLACE(DATE, '-', '') as DATE_VAL, 
                    CAM_ID, 
                    GRP_ID, 
                    TRCK,
                    case when D.CAT = 'T' then NULL else D.CAT end as CAT,          
                from DB.SCHEMA.TAB D )
                ) where  rnk = 1

4

1 回答 1

1

对于初学者,如果我做对了,您可以将上述查询简化为:

select
    REPLACE(DATE, '-', ''),
    CAM_ID,
    GRP_ID,
    first_value(NULLIF(D.CAT, 'T')) over (partition by DATE_VAL, CAM_ID, GRP_ID order by NULLIF(D.CAT, 'T') nulls last) as CATEGORY
from DB.SCHEMA.TAB D
qualify DENSE_RANK() over (partition by DATE_VAL, CAM_ID, GRP_ID order by TRCK nulls last) = 1
于 2021-08-18T14:47:31.027 回答