【问题标题】:Production Hadoop query that takes lot of time需要大量时间的生产 Hadoop 查询
【发布时间】:2019-08-30 21:37:15
【问题描述】:

当前状态

我们有一个运行了 2 个多小时的查询。在检查进度时,查询在与表 T5 的 join 和查询的最后阶段花费了大量时间。有什么方法可以简化对这个查询的处理吗?我无法使用聚合函数代替 rank(),因为使用的 orderby 有点复杂。

我们已经尝试过的

我们已经将 select 子句中的 子查询 转换为 case 语句,并帮助减少了执行时间,但这并不重要.我们简化了 T3、T4 和 T6 的相关查询。

SELECT * FROM 
        (SELECT T2.f1, T2.f2 .... T5.f19, T5.f20, 
                   case when T1.trxn_id is null then T2.crt_ts
                        when T1.trxn_id is not null and T5.acct_trxn_id is not null and T2.crt_ts >= T5.crt_ts then T2.crt_ts
                        when T1.trxn_id is not null and T5.acct_trxn_id is not null and T2.crt_ts < T5.crt_ts then T5.crt_ts
                    end as crt_ts , 
                    row_number() over ( partition by T2.w_trxn_id,
                                            if(T1.trxn_id is null, 'NULL', T1.trxn_id)
                                            order by T2.business_effective_ts desc,
                                            case when T1.trxn_id is null then T2.crt_ts
                                            when T1.trxn_id is not null and T5.acct_trxn_id is not null and T2.crt_ts >= T5.crt_ts then T2.crt_ts
                                            when T1.trxn_id is not null and T5.acct_trxn_id is not null and T2.crt_ts < T5.crt_ts then T5.crt_ts
                                            when T1.trxn_id is not null and T5.acct_trxn_id is null then T2.crt_ts end desc
                                        ) as rnk
                FROM(SELECT * FROM T3 WHERE title_name = 'CAPTURE' and tr_dt IN (SELECT tr_dt FROM DT_LKP))
                T2
                LEFT JOIN (SELECT * FROM T6 WHERE tr_dt IN (SELECT tr_dt FROM DT_LKP)) 
                T1 ON T2.w_trxn_id = T1.w_trxn_id AND T2.business_effective_ts = T1.business_effective_ts
                LEFT JOIN (SELECT f1, f3. ... f20 FROM T4 WHERE tr_dt IN (SELECT tr_dt FROM DT_LKP)) 
                T5 ON T1.trxn_id = T5.acct_trxn_id
                WHERE if(T1.trxn_id is null, 'NULL', T1.trxn_id) = if(T5.acct_trxn_id is null, 'NULL', T5.acct_trxn_id)
        ) FNL WHERE rnk = 1

【问题讨论】:

    标签: sql hadoop hive query-optimization


    【解决方案1】:

    不确定这是否对您有很大帮助。有一些比较奇怪的 WHERE 子句:

    WHERE if(T1.trxn_id is null, 'NULL', T1.trxn_id) = if(T5.acct_trxn_id is null, 'NULL', T5.acct_trxn_id)
    

    这可能是为了加入NULLs 以及正常值。然后它不起作用,因为 首先加入条件是T5 ON T1.trxn_id = T5.acct_trxn_id,这意味着NULLs没有加入,然后WHERE作为加入后的过滤器。如果T5 未加入,则 T5.acct_trxn_id 在 WHERE 中转换为“NULL”字符串,并与 NOT NULL T1.trxn_id 值进行比较,很可能会被过滤掉,在这种情况下类似于 INNER JOIN。如果它发生 T1.trxn_id 为 NULL(驱动表),它将转换为字符串 'NULL' 并与始终字符串 'NULL' 进行比较(因为根据 ON 子句无论如何都没有加入)并且这样的行通过了(虽然我没有测试它)。逻辑看起来很奇怪,我认为它不能按预期工作或转换为 INNER。如果你想加入包括 NULL 在内的所有内容,请将这个 WHERE 移到 JOIN ON 子句中。

    如果有很多行带有 NULL,则使用字符串 'NULL' 替换的 NULL 连接将增加行并导致重复。

    其实在调查 JOIN 性能不佳时,检查两件事:

    1. 连接键不重复或预期重复
    2. 连接键(以及按 row_number 中的列分区)没有倾斜,请参见:https://stackoverflow.com/a/53333652/2700344 和此:https://stackoverflow.com/a/51061613/2700344

    如果一切正常,则调整适当的减速器并行度,减少 hive.exec.reducers.bytes.per.reducer 以使更多减速器运行

    还要尽可能减少DT_LKP,即使您知道它包含一些绝对不是/不应该是事实表的日期,如果可能,请使用 CTE 对其进行过滤。

    还可以稍微简化一下逻辑(这不会提高性能,但会简化代码)。 选择中的案例:

    when T1.trxn_id is not null and T5.acct_trxn_id is not null and T2.crt_ts >= T5.crt_ts then T2.crt_ts
    when T1.trxn_id is not null and T5.acct_trxn_id is not null and T2.crt_ts < T5.crt_ts then T5.crt_ts
    

    else greatest(T2.trxn_id,T5.crt_ts)
    

    如果 T5.crt_ts 为 null,你的 case 语句将返回 null,greater() 也将返回 null

    row_number 中的 CASE 语句简化:

    case when case when (T1.trxn_id is null) or (T5.acct_trxn_id is null) then T2.crt_ts
         else greatest(T2.trxn_id,T5.crt_ts)
     end
    

    还有这个:if(T1.trxn_id is null, 'NULL', T1.trxn_id) NVL(T1.trxn_id,'NULL')

    当然这些只是建议,我没有测试它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多