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(*)是很快的。