hive多级分区和select with where子句

ss2ws0br  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(361)

我有两个分区的配置单元表,第一个分区是城市,第二个分区是村庄,所以每个城市分区都会包含所有村庄分区的列表

city1/village1
city1/village2
city1/village3
city2/village5
city2/village6

如果我的select语句是 select * from table where village = 'village5' 它会在输出结果之前搜索城市1和城市2中的所有分区吗?或者它会看到hive metastore文件并只命中village5分区。

xcitsw88

xcitsw881#

这将取决于你的Hive版本如何优化它。在我当前的版本(1.1.0)中,hive能够指向特定的分区,而无需扫描顶部分区
下面是一个快速演示。

create table mydb.partition_test 
(id string)
partitioned by (city string, village string);

INSERT OVERWRITE TABLE mydb.partition_test PARTITION (city,village)
select * from (

select '1', 'city1', 'village1'
union all 
select '1', 'city1', 'village2'
union all 
select '1', 'city1', 'village3'
union all 
select '1', 'city2', 'village5'
union all 
select '1', 'city2', 'village6'
) t;

explain select * from mydb.partition_test where village='village5';

STAGE DEPENDENCIES:
  Stage-0 is a root stage

STAGE PLANS:
  Stage: Stage-0
    Fetch Operator
      limit: -1
      Processor Tree:
        TableScan
          alias: partition_test
          filterExpr: (village = 'village5') (type: boolean)
          Statistics: Num rows: 1 Data size: 1 Basic stats: COMPLETE Column stats: PARTIAL
          Select Operator
            expressions: id (type: string), city (type: string), 'village5' (type: string)
            outputColumnNames: _col0, _col1, _col2
            Statistics: Num rows: 1 Data size: 1 Basic stats: COMPLETE Column stats: PARTIAL
            ListSink

从执行计划中可以看到,它能够估计特定分区的记录数,而无需Map操作,并且表扫描指向特定分区。

相关问题