4

如果我在具有某些分区列的表上运行下面的 hive 查询,我想确保 hive 不会进行全表扫描,而只是从元数据本身中找出结果。有什么办法可以启用它吗?

Select max(partitioned_col) from hive_table ;

现在,当我运行这个查询时,它会启动 map reduce 任务,我确信它会进行数据扫描,同时它可以很好地从元数据本身中找出值。

4

1 回答 1

4

每次更改数据时计算表统计信息。

ANALYZE TABLE hive_table PARTITION(partitioned_col) COMPUTE STATISTICS FOR COLUMNS;

启用 CBO 和统计信息自动收集:

set hive.cbo.enable=true;
set hive.stats.autogather=true;

使用这些设置来启用使用统计信息的 CBO:

set hive.compute.query.using.stats=true;
set hive.stats.fetch.partition.stats=true;
set hive.stats.fetch.column.stats=true;

如果没有任何帮助,我建议应用此方法快速查找最后一个分区: 使用表位置的 shell 脚本解析最大分区键。下面的命令将打印所有表文件夹路径、排序、采用最新排序、采用最后一个子文件夹名称、解析分区文件夹名称和提取值。您只需要初始化TABLE_DIR变量并放置the number of partition subfolder in the path

last_partition=$(hadoop fs -ls $TABLE_DIR/* | awk '{ print $8 }' | sort -r | head -n1 | cut -d / -f [number of partition subfolder in the path here] | cut -d = -f 2

然后使用$last_partition变量传递给你的脚本

  hive -hiveconf last_partition="$last_partition" -f your_script.hql
于 2017-01-31T07:54:49.620 回答