工作中有一张类型信息表,有时候需要不同的类型信息需要多次扫描这张表,表很大的时候影响效率。
优化前:
select t1.fa_id,t2.srch_char_val,t3.srch_char_val from (select fa_id from cisadm_ods.ods_cis_ci_fa_df ) t1 left join (select * from cisadm_ods.ods_cis_ci_fa_char_df where trim(char_type_cd)="aaaa") t2 on t1.fa_id=t2.fa_id left join (select * from cisadm_ods.ods_cis_ci_fa_char_df where trim(char_type_cd)="bbbb") t3 on t1.fa_id=t3.fa_id
优化后:
使用collect_set(if())这种方式对值进行条件聚合,然后把值从set中取出
select fa_id ,collect_set(if(trim(char_type_cd) = "WXSFCG",srch_char_val,null))[0] ,collect_set(if(trim(char_type_cd) = "ZYLX",srch_char_val,null))[0] from cisadm_ods.ods_cis_ci_fa_char_df where trim(char_type_cd) in ("WXSFCG","ZYLX") group by fa_id