【发布时间】: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