すべてのプロダクト
Search
ドキュメントセンター

:HyperLogLog を使用する

最終更新日:Mar 19, 2020

AnalyticDB for PostgreSQ には、Greenplum Database のネイティブ機能が組み込まれており、HyperLogLog もサポートしています。インターネット広告の分析要件や、同様の推定分析要件を持つ業界向けのソおよびその他のビジネス指標を迅速に推測することができます。

HyperLogLog 拡張機能の作成

次のコマンドを実行して HyperLogLog 拡張機能を作成します。

  1. CREATE EXTENSION hll;

基本的なタイプ

  • 次のコマンドを実行して、hll フィールドを含むテーブルを作成します。

    1. create table agg (id int primary key,userids hll);
  • 次のコマンドを実行して、整数を hll_hashval に変換します。

    1. select 1::hll_hashval;

基本的な演算子

  • hll タイプは、=、!=、<>、||、および # をサポートします。

    1. select hll_add_agg(1::hll_hashval) = hll_add_agg(2::hll_hashval);
    2. select hll_add_agg(1::hll_hashval) || hll_add_agg(2::hll_hashval);
    3. select #hll_add_agg(1::hll_hashval);
  • hll_hashval タイプは、=、!=、および <> をサポートします。

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

基本的な関数

  • サポートされている関数には、hll_hash_boolean、hll_hash_smallint、hll_hash_bigint、および他のハッシュ関数があります。

    1. select hll_hash_boolean(true);
    2. select hll_hash_integer(1);
  • hll_add_agg:hll 形式への変換に使用されます。

    1. select hll_add_agg(1::hll_hashval);
  • hll_union:hll の結合です。

    1. select hll_union(hll_add_agg(1::hll_hashval),hll_add_agg(2::hll_hashval));
  • hll_set_defaults:精度の設定に使用されます。

    1. select hll_set_defaults(15,5,-1,1);
  • hll_print:デバッグ情報に使用されます。

    1. select hll_print(hll_add_agg(1::hll_hashval));

  1. create table access_date (acc_date date unique, userids hll);
  2. insert into access_date select current_date, hll_add_agg(hll_hash_integer(user_id)) from generate_series(1,10000) t(user_id);
  3. insert into access_date select current_date-1, hll_add_agg(hll_hash_integer(user_id)) from generate_series(5000,20000) t(user_id);
  4. insert into access_date select current_date-2, hll_add_agg(hll_hash_integer(user_id)) from generate_series(9000,40000) t(user_id);
  5. postgres=# select #userids from access_date where acc_date=current_date;
  6. ?column?
  7. ------------------
  8. 9725.85273370708
  9. (1 row)
  10. postgres=# select #userids from access_date where acc_date=current_date-1;
  11. ?column?
  12. ------------------
  13. 14968.6596883279
  14. (1 row)
  15. postgres=# select #userids from access_date where acc_date=current_date-2;
  16. ?column?
  17. ------------------
  18. 29361.5209149911
  19. (1 row)