高速查询hive数据仓库表中的总条数

Author: kwu

高速查询hive数据仓库中的条数。在查询hive表的条数,通常使用count(*)。可是数据量大的时候,mr跑count(*)往往须要几分钟的时间。

1、传统方式获得总条数例如以下:

select count(*) from ods.tracklog;


执行时间为91.208s

2、与关系库一样hive表也能够通过查询元数据来得到总条数:

select d.NAME,t.TBL_NAME,t.TBL_ID,p.PART_ID,p.PART_NAME,a.PARAM_VALUE 
from TBLS t 
left join DBS d
on t.DB_ID = d.DB_ID
left join PARTITIONS p
on t.TBL_ID = p.TBL_ID 
left join PARTITION_PARAMS a
on p.PART_ID=a.PART_ID
where t.TBL_NAME='tracklog' and d.NAME='ods' and a.PARAM_KEY='numRows';


select FORMAT(sum(a.PARAM_VALUE),0)
from TBLS t 
left join DBS d
on t.DB_ID = d.DB_ID
left join PARTITIONS p
on t.TBL_ID = p.TBL_ID 
left join PARTITION_PARAMS a
on p.PART_ID=a.PART_ID
where t.TBL_NAME='tracklog' and d.NAME='ods' and a.PARAM_KEY='numRows';




仅仅需0.071s就可以返回


3、说明通过hive元数据的查询总条数,仅仅适用于有partition的表,我们正式表基本都是有partition的,仅仅有部分小表。小于1万条的没有partition,这样的小表count(*)是很快的。

posted @ 2017-05-05 09:44  claireyuancy  阅读(14542)  评论(0编辑  收藏  举报