PFCOUNT
The PFCOUNT
command in DiceDB is used to return the approximate cardinality (i.e., the number of unique elements) of the set(s) stored in the specified HyperLogLog data structure(s). HyperLogLog is a probabilistic data structure used for estimating the cardinality of a set with a high degree of accuracy and minimal memory usage.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key(s) of the HyperLogLog data structure(s) whose cardinality you want to estimate. You can specify one or more keys. | String | Yes |
Return Values
Condition | Return Value |
---|---|
The specified key exists and contains a valid HyperLogLog | The estimated number of unique elements as an integer |
The key does not exist or is not a valid HyperLogLog | 0 |
Behaviour
When the PFCOUNT
command is executed, DiceDB will:
- Retrieve the HyperLogLog data structure(s) associated with the specified key(s).
- Estimate the cardinality of the set(s) represented by the HyperLogLog data structure(s).
- If multiple keys are specified, DiceDB will merge the HyperLogLog data structures and return the cardinality of the union of the sets.
Errors
The PFCOUNT
command can raise errors in the following scenarios:
-
Non-Existent Key
: If the specified key does not exist, DiceDB will treat it as an empty HyperLogLog and return a cardinality of 0. -
Wrong Type of Key
: If the specified key exists but is not a HyperLogLog data structure, DiceDB will return a type error.Error Message
:WRONGTYPE Operation against a key holding the wrong kind of value
-
Invalid Arguments
: If the command is called with no arguments, DiceDB will return a syntax error.Error Message
:ERR wrong number of arguments for 'pfcount' command
Example usage
Single Key
Multiple Keys
Notes
- The
PFCOUNT
command provides an approximate count, not an exact count. The error rate is typically less than 1%. - HyperLogLog is particularly useful for large datasets where an exact count is not feasible due to memory constraints.
By understanding the PFCOUNT
command, you can efficiently estimate the cardinality of large sets with minimal memory usage, making it a powerful tool for various applications such as analytics and monitoring.