【问题标题】:Hive: read table partitions defined in subselectHive:读取子选择中定义的表分区
【发布时间】:2021-07-21 06:53:56
【问题描述】:

我有一个按 partitionDate 字段分区的 Hive 表。 我可以通过简单的方式读取我选择的分区

select * from myTable where partitionDate = '2000-01-01'

我的任务是动态地指定我选择的分区。 IE。首先我想从某个表中读取它,然后才运行选择到 myTable。当然,我希望使用分区的力量。

我写了一个查询,看起来像

select * from myTable mt join thatTable tt on tt.reportDate = mt.partitionDate

查询有效,但似乎未使用分区。查询工作时间过长。

我尝试了另一种方法:

select * from myTable where partitionDate in (select reportDate from thatTable)

.. 我再次看到查询工作太慢了。

有没有办法在 Hive 中实现这一点?

更新:为 myTable 创建表

CREATE TABLE `myTable`(            
  `theDate` string,            
 ')            
PARTITIONED BY (           
  `partitionDate` string) 
TBLPROPERTIES (             
  'DO_NOT_UPDATE_STATS'='true',         
  'STATS_GENERATED_VIA_STATS_TASK'='true',                
  'spark.sql.create.version'='2.2 or prior',              
  'spark.sql.sources.schema.numPartCols'='1',    
  'spark.sql.sources.schema.numParts'='2',          
  'spark.sql.sources.schema.part.0'='{"type":"struct","fields":[{"name":"theDate","type":"string","nullable":true}...         
  'spark.sql.sources.schema.part.1'='{"name":"partitionDate","type":"string","nullable":true}...',               
  'spark.sql.sources.schema.partCol.0'='partitionDate')  

【问题讨论】:

  • 您可以运行analyze table tab compute statistics; 并检查一下吗?它应该遵循分区。除非,partitionDate 的数据类型是timestamp/date,而你其他表的数据类型是string。你也可以运行explain elect * from myTable mt join thatTable tt on tt.reportDate = mt.partitionDate 并检查它是否命中分区?
  • @KoushikRoy,我已经执行了分析表 myTable(partitionDate) 计算统计信息,但没有任何效果。我仔细检查了列类型是否相同 = String.我跑了解释,没有看到用于查询的分区的证据 select * from myTable where partitionDate in (select reportDate from thatTable)
  • 驾驶室你张贴 create table 的 mytable。想看看这是否正确分区。
  • @KoushikRoy,更新了帖子。但是请看,简单查询 select * from myTable where partitionDate = '2000-01-01' 效果很好,所以看起来问题不在于分区,而在于我指定分区的方式。
  • 或者可能是收集统计数据...

标签: sql hive query-optimization partition hive-partitions


【解决方案1】:

如果您在 Tez 执行引擎上运行 Hive,请尝试

set hive.tez.dynamic.partition.pruning=true;

在JiraHIVE-7826中阅读更多详细信息和相关配置

同时尝试重写为 LEFT SEMI JOIN:

select * 
  from myTable t 
       left semi join (select distinct reportDate from thatTable) s on t.partitionDate = s.reportDate 

如果没有任何帮助,请参阅此解决方法:https://stackoverflow.com/a/56963448/2700344

或者这个:https://stackoverflow.com/a/53279839/2700344

类似问题:Hive Query is going for full table scan when filtering on the partitions from the results of subquery/joins

【讨论】:

  • 我在 MR 上运行,而不是在 TEZ 上。另外,如何将查询结果写入变量?据我了解,这是不可能的,至少在简单的 Hive QL 中是不可能的。
  • @MiamiBeach 唯一可能的方法是提供的解决方法链接。不在同一个脚本中。
猜你喜欢
  • 2020-02-06
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
相关资源
最近更新 更多