【问题标题】:Convert subqueries to Joins将子查询转换为联接
【发布时间】:2021-09-16 04:14:07
【问题描述】:

我正在努力提高我的查询性能,因为我正在处理大数据。 (许多 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;

【问题讨论】:

  • 您的查询只涉及一个表。你会想到什么样的JOIN

标签: sql join subquery impala


【解决方案1】:

您正在做的事情可以在没有嵌套上下文的情况下简化。正如 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2020-12-06
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多