【redis】HyperLogLog

1.HyperLogLog

HyperLogLog并不是一种新的数据结构,而是一种基数算法。通过HyperLogLog
可以利用极小的内存空间完成独立总数的统计,数据集可以是IP,email,ID等。
HyperLogLog提供了三个命令:pfadd,pfcount,pfmerge;

2.添加

192.168.1.7:6379> pfadd 20240620:unique:ids "uuid-1" "uuid-2" "uuid-3" "uuid-4"
(integer) 1

3.计算独立用户数。

pfcount用于计算一个或多个HyperLogLog的独立总数。

192.168.1.7:6379> pfcount 20240620:unique:ids
(integer) 4

--再插入 
192.168.1.7:6379> pfadd 20240620:unique:ids "uuid-1" "uuid-2" "uuid-3" "uuid-90"
(integer) 1
192.168.1.7:6379> pfcount 20240620:unique:ids
(integer) 5

--说明自动去重了。
--检查内存使用情况
192.168.1.7:6379> info memory 
# Memory
used_memory:848144
used_memory_human:828.27K
used_memory_rss:7745536
used_memory_rss_human:7.39M
used_memory_peak:849200
used_memory_peak_human:829.30K
used_memory_peak_perc:99.88%
used_memory_overhead:836670
used_memory_startup:786520
used_memory_dataset:11474
used_memory_dataset_perc:18.62%
total_system_memory:8181784576
total_system_memory_human:7.62G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.13
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0


vi 1.sh 
elements=""
key="20240620:unique:ids"
for i in `seq 1 100000`
do 
  elements="${elements} uuid-"${i}
  if [[ $((i%1000)) == 0 ]];
  then 
    redis-cli -h 192.168.1.7 pfadd ${key} ${elements}
	elements=""
  fi 
done  

sh 1.sh

192.168.1.7:6379> pfcount 20240620:unique:ids
(integer) 100920

--检查内存的使用情况。
192.168.1.7:6379> info memory
# Memory
used_memory:862432
used_memory_human:842.22K
used_memory_rss:7745536
used_memory_rss_human:7.39M
used_memory_peak:862432
used_memory_peak_human:842.22K
used_memory_peak_perc:100.01%
used_memory_overhead:836670
used_memory_startup:786520
used_memory_dataset:25762
used_memory_dataset_perc:33.94%
total_system_memory:8181784576
total_system_memory_human:7.62G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:8.98
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0

内存只增加大于14K;

--可以对10万个集合进行测试。
vi 2.sh 
elements=""
key="20240620:unique:ids:set"
for i in `seq 1 100000`
do 
  elements="${elements} "${i}
  if [[ $((i%1000)) == 0 ]];
  then 
    redis-cli -h 192.168.1.7 sadd ${key} ${elements}
	elements=""
  fi 
done  

sh 2.sh
--内存增加4.6M;比14K多很多。
192.168.1.7:6379> info memory 
# Memory
used_memory:5635464
used_memory_human:5.37M
used_memory_rss:12197888
used_memory_rss_human:11.63M
used_memory_peak:5635464
used_memory_peak_human:5.37M
used_memory_peak_perc:100.00%
used_memory_overhead:836710
used_memory_startup:786520
used_memory_dataset:4798754
used_memory_dataset_perc:98.96%
total_system_memory:8181784576
total_system_memory_human:7.62G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.16
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0

可以看到HyperLogLog内存占用小的惊人,但是用如此小空间来估算
如何巨大的数据,必然不是100%的正确,其中一定存在误差。redis 
官方给出的数字是0.81%的失误率。

4.合并 

--将连个hyperloglog合并成一个。
192.168.1.7:6379> pfadd 20240620:unique:ids1 "uuid-1" "uuid-2" "uuid-3" "uuid-4"
(integer) 1
192.168.1.7:6379> pfadd 20240620:unique:ids2 "uuid-4" "uuid-5" "uuid-6" "uuid-7"
(integer) 1
192.168.1.7:6379> pfmerge merge:20240620:unique:ids2 20240620:unique:ids1 20240620:unique:ids2 
OK
192.168.1.7:6379> pfcount merge:20240620:unique:ids2
(integer) 7

HyperLogLog内存占用量非常小,但是存在错误率,开发者需要确认如下条件。
只为了计算独立总数,不需要获取单条数据。
可以容忍一定误差率,毕竟HyperLogLog在内存的占用上有很大的优势。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值