【问题标题】:Hadoop - Hive - Impala - rewrite a query for performanceHadoop - Hive - Impala - 重写查询以提高性能
【发布时间】:2020-07-17 02:58:09
【问题描述】:

我有 2 个表格,下面是列

表1

col1   col2   col3     val
11     221    38       10
null   90     null     989
78     90     null     77

表2

col1   col2   col3  
12     221    78
23     null   67 
78     90     null

如果值匹配,我想首先在 col1 上加入这 2 个表,如果不匹配,则停止在 col2 上加入柱子。所以,输出应该是这样的:

col1   col2   col3     val     matchingcol
11     221    38       10      col2
null   90     null     null    null
78     90     null     77      col1

我可以使用下面的查询来做到这一点,但性能很慢。请让我知道下面是否有更好的写作方式以获得更快的性能

select *
from table1 t1 left join
     table2 t2_1
     on t2_1.col1 = t1.col1 left join
     table2 t2_2
     on t2_2.col2 = t1.col2 and t2_1.col1 
     left join table2 t2_3 on t2_3.col3 = t1.col3 and t2_2.col2 is null

ps:我之前问过同样的问题,但没有更好的答案

【问题讨论】:

  • 你能解释一下为什么你认为 Table1 的第二行不匹配,因为 col2 = 90 并且 Table2 的第三行也有 col2 = 90?

标签: sql hadoop hive impala


【解决方案1】:

你描述的是:

select t1.col1, t1.col2, t1.col3, 
       (case when t2_1.col1 is not null or t2_2.col1 is not null or t2_3.col1 is not null then t1.val end) as val
       (case when t2_1.col1 is not null then 'col1'
             when t2_2.col2 is not null then 'col2'
             when t2_3.col3 is not null then 'col3'
        end) as matching
from table1 t1 left join
     table2 t2_1
     on t2_1.col1 = t1.col1 left join
     table2 t2_2
     on t2_2.col2 = t1.col2 and t2_1.col1 is null left join
     table2 t2_3
     on t2_3.col3 = t1.col3 and t2_2.col2 is null;

这可能是最好的方法。

【讨论】:

  • 正是我现在有相同的查询,但它需要很长时间,我想知道是否有任何其他方法来编写查询
猜你喜欢
  • 2012-10-19
  • 2020-02-12
  • 2013-08-03
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多