Redis 简明教程

Redis - HyperLogLog

Redis HyperLogLog 是一种算法,它使用随机化来近似集合中唯一元素的数量,只使用一个常量和少量内存。

Redis HyperLogLog is an algorithm that uses randomization in order to provide an approximation of the number of unique elements in a set using just a constant, and small amount of memory.

即使使用极少的内存(大约每个键 12 KB),HyperLogLog 也可以很好地近似集合的基数,标准误为 0.81%。您可以计数的项目数量没有限制,除非达到 264 个项目。

HyperLogLog provides a very good approximation of the cardinality of a set even using a very small amount of memory around 12 kbytes per key with a standard error of 0.81%. There is no limit to the number of items you can count, unless you approach 264 items.

Example

下面的示例说明 Redis HyperLogLog 的工作原理。

Following example explains how Redis HyperLogLog works.

redis 127.0.0.1:6379> PFADD tutorials "redis"
1) (integer) 1
redis 127.0.0.1:6379> PFADD tutorials "mongodb"
1) (integer) 1
redis 127.0.0.1:6379> PFADD tutorials "mysql"
1) (integer) 1
redis 127.0.0.1:6379> PFCOUNT tutorials
(integer) 3

Redis HyperLogLog Commands

下表列出了一些与 Redis HyperLogLog 相关的基本命令。

Following table lists some basic commands related to Redis HyperLogLog.

Sr.No

Command & Description

1

PFADD key element [element …​]Adds the specified elements to the specified HyperLogLog.

2

PFCOUNT key [key …​]Returns the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).

3

PFMERGE destkey sourcekey [sourcekey …​]Merges N different HyperLogLogs into a single one.