使用Hive日期函数而不是硬编码日期字符串时,Hive查询性能很慢?

时间:2016-12-08 20:20:17

标签: hadoop hive hiveql

我有一个每天都会更新的事务表table_A。我每天都会使用table_A字段从外部table_Bfile_date插入新数据,以便从外部table_B过滤必要的数据以插入table_A。但是,如果我使用硬编码日期而不是使用Hive日期函数,则会产生巨大的性能差异:

-- Fast version (~20 minutes)
SET date_ingest = '2016-12-07';
SET hive.exec.dynamic.partition.mode = nonstrict;
SET hive.exec.dynamic.partition = TRUE;

INSERT
    INTO
        TABLE
            table_A PARTITION (FILE_DATE) SELECT
                    id, eventtime
                    ,CONCAT_WS( '-' ,substr ( eventtime ,0 ,4 ) ,SUBSTRING( eventtime ,5 ,2 ) ,SUBSTRING( eventtime ,7 ,2 ) )
                FROM
                    table_B
                WHERE
                    file_date = ${hiveconf:date_ingest}
;

与之相比:

-- Slow version (~9 hours)
SET date_ingest = date_add(to_date(from_unixtime( unix_timestamp( ) )),-1);
SET hive.exec.dynamic.partition.mode = nonstrict;
SET hive.exec.dynamic.partition = TRUE;

INSERT
    INTO
        TABLE
            table_A PARTITION (FILE_DATE) SELECT
                    id, eventtime
                    ,CONCAT_WS( '-' ,substr ( eventtime ,0 ,4 ) ,SUBSTRING( eventtime ,5 ,2 ) ,SUBSTRING( eventtime ,7 ,2 ) )
                FROM
                    table_B
                WHERE
                    file_date = ${hiveconf:date_ingest}
;

有没有人遇到过类似的问题?您应该假设我无法访问Unix hive命令(即不能使用--hiveconf选项),因为我们正在使用第三方UI。

1 个答案:

答案 0 :(得分:0)

在filter子句中使用函数时,有时分区修剪不起作用。如果您在包装器shell脚本中计算变量并将其作为-hiveconf变量传递给Hive,它将正常工作。 例如:

#inside shell script
date_ingest=$(date -d '-1 day' +%Y-%m-%d)
hive -f your_script.hql -hiveconf date_ingest="$date_ingest" 

然后在Hive脚本中将其用作WHERE file_date ='${hiveconf:date_ingest}'