首页 > 解决方案 > 将子查询转换为联接

问题描述

我正在尝试提高我的查询性能,因为我正在处理大数据。(许多 TB 的数据)我正在研究 Impala。

我使用的查询如下所述。这是子查询的形式。不知道如何优化它。可能是加入。如果是,我无法将此子查询转换为 JOINS。

请建议如何将此子查询转换为 JOINS。我需要提高我的查询性能

select mytable.id,group_concat(mytable.acqid) as 'acqid',group_concat(mytable.address) as 'address'
from (select myinnertable.id, case when myinnertable.mykey = 'AcqId' then myinnertable.myvalue end as 'acqid',
             case when myinnertable.mykey = 'Address' then myinnertable.myvalue end as 'address'
      from (select id,mykey,myvalue
            from table1
            where table1.date_col BETWEEN '2021-05-02' and '2021-05-19' and mykey!='velList'
           ) myinnertable
     ) mytable
group by mytable.id;

标签: sqljoinsubqueryimpala

解决方案


您正在做的事情可以在没有嵌套上下文的情况下简化。正如 Gordon 指出的那样,您仅使用 1 个表,但通过结果进行了 3 次传递。我会确保它有一个索引(date_col, id, mykey)来帮助优化拉取数据。

select 
        t1.id, 
        group_concat( case when t1.mykey = 'AcqId' 
                        then t1.myvalue end ) acqid,
        group_concat( case when t1.mykey = 'Address' 
                        then t1.myvalue end ) address
    from 
        table1 t1
    where 
            t1.date_col BETWEEN '2021-05-02' and '2021-05-19' 
        and t1.mykey != 'velList' 
    group by 
        t1.id;

推荐阅读