当前位置:网站首页>Redis6新数据类型——HyperLogLog
Redis6新数据类型——HyperLogLog
2022-07-17 07:23:00 【幻清】
1. 简介
在工作当中,我们经常会遇到与统计相关的功能需求,比如统计网站 PV (PageView 页面访问量),可以使用 Redis 的 incr、incrby 轻松实现。 但像 UV(UniqueVisitor,独立访客)、独立 IP 数、搜索记录数等需要去重和 计数的问题如何解决?这种求集合中不重复元素个数的问题称为基数问题。 解决基数问题有很多种方案:
(1)数据存储在 MySQL 表中,使用 distinct count 计算不重复个数
(2)使用 Redis 提供的 hash、set、bitmaps 等数据结构来处理
以上的方案结果精确,但随着数据不断增加,导致占用空间越来越大,对于非 常大的数据集是不切实际的。
能否能够降低一定的精度来平衡存储空间?Redis 推出了 HyperLogLog
Redis HyperLogLog 是用来做基数统计的算法,HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定的、并且是很小 的。
在 Redis 里面,每个 HyperLogLog 键只需要花费 12 KB 内存,就可以计算接 近 2^64 个不同元素的基数。这和计算基数时,元素越多耗费内存就越多的集合形成鲜明对比。
但是,因为 HyperLogLog 只会根据输入元素来计算基数,而不会储存输入元素 本身,所以 HyperLogLog 不能像集合那样,返回输入的各个元素。
什么是基数?
比如数据集 {1, 3, 5, 7, 5, 7, 8}, 那么这个数据集的基数集为 {1, 3, 5 ,7, 8}, 基数(不重复元素)为 5。 基数估计就是在误差可接受的范围内,快速计算基数。
2. 命令
1、pfadd
(1)格式 pfadd <key>< element> [element ...] 添加指定元素到 HyperLogLog 中
(2)实例

将所有元素添加到指定HyperLogLog数据结构中。如果执行命令后HLL估计的近似基数发生变化,则返回1,否则返回0。
2、pfcount
(1)格式pfcount<key> [key ...] 计算HLL的近似基数,可以计算多个HLL,比如用HLL存储每天的UV,计算一周的UV可以使用7天的UV合并计算即可

(2)实例
3、pfmerge
(1)格式pfmerge<destkey><sourcekey> [sourcekey ...] 将一个或多个HLL合并后的结果存储在另一个HLL中,比如每月活跃用户可以使用每天的活跃用户来合并计算可得
(2)实例
边栏推荐
- Why does the Fed cut interest rates benefit the digital money market in the long run? 2020-03-05
- 5.1 security vulnerabilities and Prevention
- 全志V3s学习记录(13)OV2640的使用
- STM32F103C8T6硬件IIC控制4针0.96寸OLED显示屏
- Conversation technology [dark horse introduction series]
- How to use curl in Jenkins pipeline and process response results
- LeetCode 每日一题 2021/7/11-2021/7/17
- Visual studio production environment configuration scheme: slowcheetah
- [C classes and objects] - Methods and class and object programming in C
- 5.2 数据库安全
猜你喜欢

Application of SCA on devsecops platform
![[C # variable constant keyword] - variable constants and keywords in C #](/img/9b/433f9110d9c7599d8beac8288ea409.png)
[C # variable constant keyword] - variable constants and keywords in C #

数据库写入优化:分库分表及相关问题

oop_引用类型变量传值

812. 最大三角形面积

类型详解·自定义类型·结构体初识

Wvppro-zlm-gb21818-camera

總結的太好了!終於有人把SQL的各種連接Join都講明白了

Look back at the paper after reading the code: yolov3 reread

Visual studio production environment configuration scheme: slowcheetah
随机推荐
Redis cluster
LeetCode 每日一题 2021/7/11-2021/7/17
leetcode:287. 寻找重复数【快慢指针板子】
MySQL 2502 2503 error
C # read and write txt files
How to use curl in Jenkins pipeline and process response results
ObjectARX--自定义圆的实现
Yolov5 label and establish your own data set
OI回忆录
Filesourcestrategy, datasourcestrategy and datasourcev2strategy in spark
Redis distributed lock
Look back at the paper after reading the code: yolov3 reread
Solution to sudo PIP install gevent installation failure
SCA在得物DevSecOps平台上应用
Redis cache avalanche, penetration, breakdown
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
Database review -- database recovery technology
How to check whether the app has user information and data leakage vulnerabilities
经典通用的Pbootcms花卉网站模板源码,自适应手机端,带后台管理
be vigilant! Another phishing attack: uniswap stolen $8.1 million