AnalyticDB for PostgreSQ には、Greenplum Database のネイティブ機能が組み込まれており、HyperLogLog もサポートしています。インターネット広告の分析要件や、同様の推定分析要件を持つ業界向けのソおよびその他のビジネス指標を迅速に推測することができます。
HyperLogLog 拡張機能の作成
次のコマンドを実行して HyperLogLog 拡張機能を作成します。
CREATE EXTENSION hll;
基本的なタイプ
次のコマンドを実行して、hll フィールドを含むテーブルを作成します。
create table agg (id int primary key,userids hll);
次のコマンドを実行して、整数を 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、および他のハッシュ関数があります。
select hll_hash_boolean(true);
select hll_hash_integer(1);
hll_add_agg: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:デバッグ情報に使用されます。
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)