hyper运算符_HyperLogLog 的使用

阿里云深度优化云数据库 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值