hyper運算子 HyperLogLog 的使用

2021-10-12 15:35:12 字數 2084 閱讀 3330

阿里雲深度優化雲資料庫 analyticdb for postgresql,除原生 greenplum database 功能外,還支援 hyperloglog,為網際網路廣告分析及有類似預估分析計算需求的行業提供解決方案,以便於快速預估 pv、uv 等業務指標。

建立 hyperloglog 外掛程式

執行如下命令,建立 hyperloglog 外掛程式:

create extension hll;

基本型別

執行如下命令,建立乙個含有 hll 欄位的表: create table agg (id int primary key,userids hll);

執行如下命令,進行 int 轉 hll_hashval: select 1::hll_hashval;

基本操作符

hll 型別支援 =、!=、<>、|| 和 #。 select hll_add_agg(1::hll_hashval) = hll_add_agg(2::hll_hashval);

select hll_add_agg(1::hll_hashval) || hll_add_agg(2::hll_hashval);

select #hll_add_agg(1::hll_hashval);

hll_hashval 型別支援 =、!= 和 <>。 select 1::hll_hashval = 2::hll_hashval;

select 1::hll_hashval <> 2::hll_hashval;

基本函式

hll_hash_boolean、hll_hash_smallint 和 hll_hash_bigint 等 hash 函式。 select hll_hash_boolean(true);

select hll_hash_integer(1);

hll_add_agg:可以將 int 轉 hll 格式。 select hll_add_agg(1::hll_hashval);

hll_union:hll 並集。 select hll_union(hll_add_agg(1::hll_hashval),hll_add_agg(2::hll_hashval));

hll_set_defaults:設定精度。 select hll_set_defaults(15,5,-1,1);

hll_print:用於 debug 資訊。 select hll_print(hll_add_agg(1::hll_hashval));

示例create table access_date (acc_date date unique, userids hll);

insert into access_date select current_date, hll_add_agg(hll_hash_integer(user_id)) from generate_series(1,10000) t(user_id);

insert into access_date select current_date-1, hll_add_agg(hll_hash_integer(user_id)) from generate_series(5000,20000) t(user_id);

insert into access_date select current_date-2, hll_add_agg(hll_hash_integer(user_id)) from generate_series(9000,40000) t(user_id);

postgres=# select #userids from access_date where acc_date=current_date;

?column?

9725.85273370708

(1 row)

postgres=# select #userids from access_date where acc_date=current_date-1;

?column?

14968.6596883279

(1 row)

postgres=# select #userids from access_date where acc_date=current_date-2;

?column?

29361.5209149911

(1 row)

hyper運算子 origin中基本的運算子

基礎數學函式 prec x,p 精度函式,返回x的p位有效數字。如prec 1234567,3 1.23e6 round x,p 設定小數字數 abs x 絕對值 angle x,y 原點 0,0 到 x,y 連線與正x軸夾角 exp x 指數函式 sqrt x 開方函式 ln x 自然對數函式 l...

(運算子) 運算子

運算子既可作為一元運算子也可作為二元運算子。備註 unsafe context data guid 00bf87717d88a9fac1afadb796c675da 一元 運算子返回運算元的位址 要求 unsafe 上下文 bool data guid 9efd189df2cfb88799dca08...

JS運算子 算術運算子 比較運算子 賦值運算子

兩邊的變數都是number型別 則是單純的加法運算 當字串出現時 結果是字串型別 字串之後的內容 不論什麼型別 都會被作為字串進行拼接 例子 var num1 10 var num2 20 num num1 num2 var result num1 num2 num1 false console.l...