多次查看执行

xurqigkl  于 2021-06-28  发布在  Hive
关注(0)|答案(2)|浏览(198)

我正在处理大型数据集,我需要优化查询。我有一个观点。下面的查询视图被使用了4次。所以每次执行视图时,它都包含复杂的逻辑。如何构建视图只执行一次的查询。

Select * from TableA a
    join abc_view v on(a.col1=v.line)
    where v.type='abc' 
union all
    Select * from TableA a
    join abc_view v on(a.col1=v.group)
    where v.type='bcd'
union all
    Select * from TableA a
    join abc_view v on(a.col1=v.cat)
    where v.type='cde'
union all
    Select * from TableA a
    join abc_view v on(a.col1=v.test)
    where v.type='def'

执行查询大约需要5分钟。我认为我必须从\u视图中创建一个表,并在查询中使用该表来优化它还是什么?
建议我优化查询。

yptwkmov

yptwkmov1#

试试这个,希望它能表现好

SELECT * FROM TableA a
JOIN abc_view v ON a.col1 IN (v.line, v.group, v.cat, v.test)
    AND v.type IN ('abc', 'bcd', 'cde', 'def')

索引到a.col1和视图的所有四个部分(v.line、v.group、v.cat、v.test)

mefy6pfw

mefy6pfw2#

一次对视图的引用可能会提高性能,但不能保证。在标准sql中,可以执行以下操作:

Select *
from TableA a join
     abc_view v
     on (a.col1 = v.line and v.type = 'abc' ) or
        (a.col1 = v.group and v.type = 'bcd' ) or
        (a.col1 = v.cat and v.type = 'cde' ) or
        (a.col1 = v.test and v.type = 'def' );

然而,Hive可能会拒绝这个。
我不确定Hive是否实现了ctes。如果是这样,这可能会解决您的问题:

with v as (select * from abc_view)
 Select *
 from TableA a join
      v
      on( a.col1 = v.line
 where v.type='abc' 
 union all
 Select *
 from TableA a join
      v
     on a.col1 = v.group
     where v.type = 'bcd'
 union all
 Select *
 from TableA a join
      v
      on a.col1 = v.cat
 where v.type = 'cde'
 union all
 Select *
 from TableA a join
      v
      on a.col1 = v.test
 where v.type = 'def';

否则,您可能需要使用临时表。

相关问题