【问题标题】:Optimize the join performance with Hive partition table使用 Hive 分区表优化连接性能
【发布时间】:2019-07-08 16:43:56
【问题描述】:

我有一个 Hive orc test_dev_db.TransactionUpdateTable 表,其中包含一些示例数据,其中包含需要更新到主表 (test_dev_db.TransactionMainHistoryTable) 的增量数据,该表在 Country、Tran_date 列上进行分区。

Hive 增量加载表模式:它包含需要合并的 19 行。

CREATE TABLE IF NOT EXISTS test_dev_db.TransactionUpdateTable 
(
Transaction_date timestamp,
Product       string,
Price         int,
Payment_Type  string,
Name          string, 
City          string,
State         string,
Country       string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS orc
;

Hive 主表架构:总行数 77。

CREATE TABLE IF NOT EXISTS test_dev_db.TransactionMainHistoryTable
(
Transaction_date timestamp,
Product       string,
Price         int,
Payment_Type  string,
Name          string,
City          string,
State         string
)
PARTITIONED BY (Country string,Tran_date string) 
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS orc
;

我在查询下方运行以将增量数据与主表合并。

SELECT
  case when i.transaction_date is not null then cast(substring(current_timestamp(),0,19) as timestamp)  
  else t.transaction_date   end as transaction_date,
  t.product,
  case when i.price is not null then i.price else t.price end as price,
  t.payment_type,
  t.name,
  t.city,
  t.state,
  t.country,
  case when i.transaction_date is not null then substring(current_timestamp(),0,10) 
  else t.tran_date end as tran_date
  from
test_dev_db.TransactionMainHistoryTable t
full join test_dev_db.TransactionUpdateTable i on (t.Name=i.Name)
;
/hdfs/path/database/test_dev_db.db/transactionmainhistorytable/country=Australia/tran_date=2009-03-01
/hdfs/path/database/test_dev_db.db/transactionmainhistorytable/country=Australia/tran_date=2009-05-01

并在查询下运行以过滤掉需要合并的特定分区,以消除重写未更新的分区。

SELECT
  case when i.transaction_date is not null then cast(substring(current_timestamp(),0,19) as timestamp)  
  else t.transaction_date   end as transaction_date,
  t.product,
  case when i.price is not null then i.price else t.price end as price,
  t.payment_type,
  t.name,
  t.city,
  t.state,
  t.country,
  case when i.transaction_date is not null then substring(current_timestamp(),0,10) else t.tran_date end as tran_date
  from
(SELECT 
  *
  FROM 
test_dev_db.TransactionMainHistoryTable
where Tran_date in
(select distinct  from_unixtime(to_unix_timestamp (Transaction_date,'yyyy-MM-dd HH:mm'),'yyyy-MM-dd') from test_dev_db.TransactionUpdateTable
))t
full join test_dev_db.TransactionUpdateTable i on (t.Name=i.Name)
;

只有 Transaction_date、Price 和分区列 tran_date 在这两种情况下都需要更新。两个查询运行良好,但横向执行时间较长。

分区表的执行计划为:

 Stage: Stage-5
    Map Reduce
      Map Operator Tree:
          TableScan
            alias: transactionmainhistorytable
            filterExpr: tran_date is not null (type: boolean)
            Statistics: Num rows: 77 Data size: 39151 Basic stats: COMPLETE Column stats: COMPLETE
            Map Join Operator
              condition map:
                   Left Semi Join 0 to 1
              keys:
                0 tran_date (type: string)
                1 _col0 (type: string)
              outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6, _col7, _col8

我对第二个查询做错了吗?我是否需要同时使用分区列来进行更好的修剪。 非常感谢任何帮助或建议。

【问题讨论】:

    标签: performance hive query-optimization partitioning


    【解决方案1】:

    也许这不是一个完整的答案,但我希望这些想法会有用。

    where tran_date IN (select ... )
    

    其实和

    是一样的
    LEFT SEMI JOIN (SELECT ...)
    

    这反映在计划中:

    Map Join Operator
                  condition map:
                       Left Semi Join 0 to 1
                  keys:
                    0 tran_date (type: string)
                    1 _col0 (type: string) 
    

    它作为 map-join 执行。首先选择子查询数据集,其次将其放置在分布式缓存中,加载到内存中以用于映射连接。所有这些步骤:选择、加载到内存、map-join 比读取和覆盖所有表要慢,因为它太小且过度分区:统计数据显示 Num rows: 77 Data size: 39151 - 太小而不能被两个分区列甚至太小而根本无法分区。尝试更大的表并使用 EXPLAIN EXTENDED 来检查真正被扫描的内容。

    另外,替换这个:

    from_unixtime(to_unix_timestamp (Transaction_date,'yyyy-MM-dd HH:mm'),'yyyy-MM-dd')
    

    substr(Transaction_date,0,10)date(Transaction_date)

    还有substring(current_timestamp,0,10)current_date 只是为了简化代码。

    如果您希望在计划中显示分区过滤器,请尝试替换作为分区列表传递的分区过滤器,您可以在单独的会话中选择它并使用 shell 将分区列表传递到 where 子句,请参阅此答案: https://stackoverflow.com/a/56963448/2700344

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      相关资源
      最近更新 更多