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

Redis data manipulation-the difference between string and hash key

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

| if string keys can do what hash keys can do, why don't we just use string keys? | benefits of hashing-putting data in the same place # Hash allows us to store some related information in the same place instead of directly scattered in the whole data, which not only facilitates data management, but also avoids misoperation as far as possible. # for example, to delete the message information recorded by the string key, we need to enter three keys, and to delete the message information stored by the hash key, we only need to enter a key string key: id > > 10086; sender > > peter; receiver > > jack hash key: message > > (id > 10086; sender > > peter Receiver > > jack)-avoid key name conflicts # when naming keys in the introduction of string keys, use delimiters to avoid naming conflicts, but the best way is to directly use hash keys to store almost data # directly use message: hash keys to moisturize message self-information, and store all information about ed in messages in various fields of hash. # ensures that the function of each key in the database is fixed, single, and the stored information is isolated, thus avoiding key name conflicts to the maximum extent. # as the number of and increases, using only hashes will create many fewer database keys than using strings. -- reduce memory footprint # in general, save the same number of key-value pairs of information, using hash keys saves more memory than using string keys. # because each key created in the database contains additional management information in the database (such as the type of the key, the time of the last access, etc.), the more keys in the database, the more memory the server consumes in storing additional management information, and the more CPU is spent on managing database keys. # when the hash contains a small number of domain value pairs, Redis will automatically use a data structure that takes up very little memory to do the hash. the bottom layer of the hash is Southwest. When the number of hashes is large, this measure is of great help to reduce memory. Conclusion whenever possible, use hash keys instead of string keys to store key-value pairs, because hash keys are easy to manage, avoid key-name conflicts, and save memory. Some cases where there is no way to use hash keys instead of string keys. Use binary operation commands: because Redis currently supports operations such as setbit,getbit,bitop on strings, if you want to use these operations, name can only use string keys. two。 Use expiration function: Redis's key expiration function can only expire keys, not hash fields, so if you want to use the expiration function for key value pairs, you can only store the pair in a string.

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

Database

Wechat

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

12
Report