In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the Redis data structure and data operation example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
The speed of Redis to complete data operations can reach the level of microseconds. There are two main reasons why Redis can perform so well:
Redis is an in-memory database, all operations are done in memory, and the access speed of memory itself is very fast.
Redis has efficient data types and data structures.
In order to achieve fast access from key to value, Redis uses a hash table to store key-value pairs. Entry in the hash bucket holds pointers to the actual key and value, even if the value is a collection, it can be found by value pointer lookup.
When there is more and more data in the hash table, there will be a hash conflict, that is, the hash values of multiple key may correspond to the same hash bucket. Redis uses chained hashes to resolve hash conflicts, that is, multiple elements in the same hash bucket are stored in a linked list, and the elements are linked by pointers in turn.
If there are more and more hash conflicts, the hash conflict chain will be too long, which will lead to time-consuming and inefficient searching for elements. To solve this problem, Redis rehash the hash table, storing multiple entry elements separately, reducing the number of elements in a single hash bucket, thereby reducing conflicts in a single bucket.
By default, Redis uses two global hash tables for efficient rehash. At first, hash table 1 is used by default, and hash table 2 does not allocate space. When the data is increasing, redis uses the following steps to rehash:
Allocate more space to hash table 2
Copy the data from hash table 1 to hash table 2
Free the space in hash table 1 for the next rehash expansion
However, in step 2, if a large amount of data is copied at once, the Redis thread may be blocked and unable to serve other requests, so Redis adopts progressive rehash, which is to copy all the entry at this index location for each request processed.
For value of type String, the CRUD operation can be performed directly when the hash bucket is found, while for the collection, the corresponding hash bucket is found through the global hash table, and then CRUD is performed in the collection. The operational efficiency of the set is related to the underlying data structure and operational complexity.
Single element operation is the foundation, and the operation complexity is O (1).
Hash:HGET 、 HSET 、 HDEL
SADD, SREM, SRANDMEMBER and so on of Set type.
The range operation is very time-consuming and the operation complexity is O (N).
Hash:HGETALL
Set:SMEMBERS
List:LRANGE
ZSet:ZRANGE
The statistical operation is usually efficient and the operation complexity is O (1).
There are only a few exceptions, and the operation complexity is O (1).
List:LPOP 、 RPOP 、 LPUSH 、 RPUSH
The above is all the contents of the article "sample Analysis of data structures and data Operations in Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.