Redis重制(十)Hyperloglog

9 阅读1分钟

这里我们来学习redis的第七个数据类型:hyperloglog(数据结构)

 

Redis Hyperloglog:基数统计算法

 

网页访问量:一个人访问一个网站多次,但是还是算作一个人!

传统的方式,set保存用户的id,然后就可以统计set中的元素数量作为标准判断!这个方式如果保存大量的用户id,就会比较麻烦!占用内存空间太多。我们的目的是为了计数,而不是保存用户d:

 

Hyperloglog优点:

占用内存固定:2^64次方不同的元素存储,只需要占用12k内存。如果要从内存角度来比较的话Hyperloglog首选!

 

Hyperloglog缺点:

大概有0.81%得错误率(数据量特别大的时候)

 

1:PFadd创建key

127.0.0.1:6379pfadd hyper a b c d e f g h j k l m n b v c x
(integer) 1
127.0.0.1:6379pfadd hyper1  1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 2 3
(integer) 1

 

2:PFCOUNT 查看key值数量

127.0.0.1:6379> pfcount hyper
(integer) 15
127.0.0.1:6379> pfcount hyper1
(integer) 10

 

3:PFMERGE合并两个key

127.0.0.1:6379> pfmerge hyper hyper1
OK
127.0.0.1:6379> pfcount hyper1
(integer) 10
127.0.0.1:6379> pfcount hyper
(integer) 25
127.0.0.1:6379>

将hyper1中的元素合并到hyper中。

 

拢共就这三个方法。很简单

 

Hyerloglog用于一般的不需要特别精确的数据统计。占用内存小,查询还快。

 

以上大概就是hyperloglog的基本使用。

 

有好的建议,请在下方输入你的评论。