clickhouse聚合函式之groupBitmap
阿新 • • 發佈:2021-01-18
技術標籤:clickhousegroupBitmapclickhouse
用途
官方點就是計算基數
,直白點就是統計不重複的個數
引數為Uint
型別,就是不是負數的整數
返回值為Uint64
型別
案例
測試資料
UserID
1
1
2
3
SELECT groupBitmap(UserID) as num FROM t;
結果
num
3
再舉個例子,比如我們有使用者id uid
, string
型別,相要統計其個數,則
SELECT groupBitmap(toUInt64(uid)) as num from usertable;
+----+
|num |
+----+
|2399 |
+----+
等價於如下查詢
select count(distinct uid) from usertable;
那麼要bitmap
做什麼呢?就是建立表時如果選擇bitmap
儲存,就會比較節約空間
比如
create table testbit(
label String,
name String,
uv AggregateFunction(groupBitmap,UInt64) comment 'bitmap儲存使用者'
)engine=AggregatingMergeTree()
partition by label
order by (label,name) ;
groupBitmapState
可以看出返回值型別是AggregateFunction(groupBitmap,UInt64)
SELECT groupBitmapState(toUInt64(uid)) as num,
toTypeName(num)
from usertable;
想要知道bitmap儲存的是什麼東西,直接查是看不出來的,需要轉為陣列才可以,比如
select bitmapToArray(groupBitmapState(toUInt64(uid))) from usertable;
如果想求基數
,如下
select bitmapCardinality( groupBitmapState(toUInt64(uid))) from usertable;
+--------------------------------------------------+
|bitmapCardinality(groupBitmapState(toUInt64(uid)))|
+--------------------------------------------------+
|2399 |
+--------------------------------------------------+