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


Author: kwu

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

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

select count(*) from ods.tracklog;


运行时间为91.208s

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

<pre name="code" class="sql">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(*)是非常快的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关内容