Skip to content

Instantly share code, notes, and snippets.

@justdoit0823
Created July 18, 2017 04:38
Show Gist options
  • Save justdoit0823/ccf86b2d1ebf2dcb32b5c0ee50133b42 to your computer and use it in GitHub Desktop.
Save justdoit0823/ccf86b2d1ebf2dcb32b5c0ee50133b42 to your computer and use it in GitHub Desktop.
How hll works in postgresql
drop table if exists s_user_table;
create table s_user_table (
id int,
user_id int
);
drop table if exists s_user_cnt_table;
create table s_user_cnt_table (
user_id int,
cnt hll
);
insert into s_user_table select id, id from generate_series(1, 987654) as id;
insert into s_user_cnt_table select user_id % 1000, hll_add_agg(hll_hash_integer(user_id)) from s_user_table group by 1;
with A(user_id, cnt) as (
select user_id, hll_cardinality(cnt) from s_user_cnt_table limit 10),
B(user_id, cnt) as (
select user_id % 1000, count(id) from s_user_table where user_id % 1000 in (select user_id from A) group by 1)
select A.user_id, A.cnt, B.cnt, abs(A.cnt - B.cnt) / B.cnt from A, B where A.user_id = B.user_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment