Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of HyperLogLog in Redis

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the role of HyperLogLog in Redis? aiming at this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

HyperLogLog is the advanced data structure of Redis, which is very useful when doing cardinality statistics. The key of each HyperLogLog can calculate the cardinality of different elements close to 264, and the size only needs 12KB.

PFADD

Earliest available version: 2.8.9

Time complexity: O (1)

Adding all the elements in the parameter to the specified HyperLogLog data structure, this command affects the cardinality calculation. If the cardinality estimate changes after executing the command, 1 is returned; otherwise, 0 is returned. If the specified key does not exist, an empty HyperLogLog data structure is created. The command also supports specifying a key value instead of an element, creating a new HyperLogLog data structure if it does not exist and returning 1; otherwise, 0.

PFCOUNT

Earliest available version: 2.8.9

Time complexity: O (1), for several large key, the time complexity is O (N).

For a single key, this command returns the approximate cardinality of the specified key, or 0 if the variable does not exist.

For multiple key, the approximate cardinality of multiple HyperLogLog unions is returned, which is calculated by merging multiple HyperLogLog into a temporary HyperLogLog.

HyperLogLog can store unique elements of a collection with very little memory. (only 12K per HyperLogLog plus a few bytes of key itself)

HyperLogLog's results are not accurate, with an error rate of about 0.81%.

It is important to note that this command changes the HyperLogLog, so 8 bytes are used to store the cardinality of the last calculation. So, technically speaking, PFCOUNT is a write command.

Performance problem

Even though it theoretically takes a long time to process a HyperLogLog with high storage density, the PFCOUNT command still has high performance when specifying a key. This is because PFCOUNT caches the cardinality of the last settlement, and most PFADD commands do not update registers. So it can achieve the effect of hundreds of requests per second.

When dealing with multiple key, the most time-consuming step is the merge operation. The cardinality of the union calculated cannot be cached. So the processing speed of multiple key is generally in millisecond level.

PFMERGE

Earliest available version: 2.8.9

Time complexity: O (N), N is the number of HyperLogLog to be merged

Usage: PFMERGE destkey sourcekey [sourcekey …]

Multiple HyperLogLog are merged, and the cardinality after merging is similar to the observed Sets of the cardinality before merging. After the calculation, the result is saved to the specified key.

In addition to these three commands, we can also use SET and GET commands on HyperLogLog data as we do with data of type String.

The answer to the question about the role of HyperLogLog in Redis is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report