按字段值拆分子表上的配置单元表

lymnna71  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(255)

我有一张Hive桌 foo . 此表中有几个字段。其中之一就是 some_id . 此字段中唯一值的数目,范围为5000-10000。对于每个值(例如 10385 )我需要表演 CTAS 像这样的查询

  1. CREATE TABLE bar_10385 AS
  2. SELECT * FROM foo WHERE some_id=10385 AND other_id=10385;

执行这一系列查询的最佳方法是什么?

nue99wik

nue99wik1#

您可以将所有这些表存储在单个分区的表中。这种方法允许您在单个查询中加载所有数据。查询性能不会受到影响。

  1. Create table T (
  2. ... --columns here
  3. )
  4. partitioned by (id int); --new calculated partition key

使用一个查询加载数据,它将只读取源表一次:

  1. insert overwrite table T partition(id)
  2. select ..., --columns
  3. case when some_id=10385 AND other_id=10385 then 10385
  4. when some_id=10386 AND other_id=10386 then 10386
  5. ...
  6. --and so on
  7. else 0 --default partition for records not attributed
  8. end as id --partition column
  9. from foo
  10. where some_id in (10385,10386) AND other_id in (10385,10386) --filter

然后可以在指定分区的查询中使用此表:

  1. select from T where id = 10385; --you can create a view named bar_10385, it will act the same as your table. Partition pruning works fast
展开查看全部

相关问题