Hive SQL解决读取分区字符不规范导致全表扫描的问题
接手旧的项目,看到下面的hive sql
INSERT overwrite table saas.ods_t_iot partition(ds=${yesdate})
select pid,ptime,pvalue from sass.ods_t_iot_source b
where b.dt=${yes_date} and pid like '10%'
union all
select pid,ptime,pvalue from sass.ods_t_iot_source a
where a.dt=${yes_date} and pid like 'IOT.10%';
执行时间居然要5、6个小时以上才跑出结果,虽然sass.ods_t_iot_source每天有9亿多条数据,但是简单的查询和插入,没有复杂计算和shuffle,但觉不应该这样才对。
后来排查发现,ds=${yesdate},例如
yesdate 是2024-08-05,替换为变量的值是ds=2024-08-05,但由于ds实际分区日期是字符串,上面会导致全表扫描sass.ods_t_iot_source的数据,而不是只读某一天的分区。由于 sass.ods_t_iot_source 有多天数据,数量庞大,所以效率才会这样低下。
修改后的sql如下:
INSERT overwrite table saas.ods_t_iot partition(ds='${yesdate}')
select pid,ptime,pvalue from sass.ods_t_iot_source b
where b.dt='${yes_date}' and pid like '10%'
union all
select pid,ptime,pvalue from sass.ods_t_iot_source a
where a.dt='${yes_date}' and pid like 'IOT.10%';
修改后几分钟后就跑出结果。