In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The Redis key command is used to manage the keys of redis. How does redis quickly traverse all key? I believe there are many people do not understand, today, in order to let you know more about redis quickly traversing all key methods, so give you a summary of the following content, let's look down.
When we need to iterate through all the key of Redis or the key of a specified mode, the first thing that comes to mind is the KEYS command:
KEYS pattern
The official website has a hint for the KEYS command: KEYS is very fast. For example, it takes 40 milliseconds for Redis to execute a query in a database of 1 million key. But using it in a large database can still cause performance problems, and if you need to find a specific KEYS from a dataset, you'd better use Redis's collection structure SETS instead.
The KEYS command is easy to use.
Redis > MSET one 1 two 2 three 3 four 4OKredis > KEYS * ofe1) "four" 2) "one" 3) "two" redis > KEYS tweets 1) "two" redis > KEYS * 1) "four" 2) "three" 3) "one" 4) "two" redis >
However, because the KEYS command returns all matching key at once, when there are a lot of key in redis, it is a hidden trouble for memory consumption and redis server.
For Redis version 2.8 and above, it provides us with a better command to traverse key SCAN the basic format of the command:
SCAN cursor [MATCH pattern] [COUNT count]
SCAN returns only a small number of elements per execution, so it can be used in a production environment without problems like KEYS or SMEMBERS commands that can block the server.
The SCAN command is a cursor-based iterator. This means that each time the command is called, it needs to use the cursor returned by the last call as the cursor parameter of the call, so as to continue the previous iterative process.
When the cursor parameter (that is, cursor) of the SCAN command is set to 0, the server starts a new iteration, and when the server returns a cursor with a value of 0 to the user, the iteration is over.
A simple iterative demonstration:
Redis 127.0.0.1 key:4 6379 > scan 01) "17" 2) 1) "key:12" 2) "key:8" 3) "key:4" 4) "key:14" 5) "key:16" 6) "key:17" 7) "key:15" 8) "key:10" 9) "key:3" 10) "key:7" 11) "key:1" redis 127.0.0.1 scan) 1) "key:5" 2) "key:18" 3) "key:0" 4) "key:2" 5) "key:19" 6) "key:13" 7) "key:6" 8) "key:9" 9) "key:11"
In the above example, the first iteration uses 0 as the cursor to start a new iteration. The second iteration uses the cursor 17 returned from the first iteration as a new iteration parameter.
Obviously, the return value of the SCAN command is an array of two elements, the first array element is a new cursor for the next iteration, and the second array element is an array containing all the iterated elements.
Note: the returned cursor is not necessarily incremented, the next returned cursor may be smaller than the previous one.
On the second call to the SCAN command, the command returns cursor 0, which means that the iteration is over and the entire dataset has been fully traversed.
Full iteration: start a new iteration with 0 as the cursor, calling the SCAN command until the command returns cursor 0, which we call a full traversal.
The SCAN incremental iteration command does not guarantee that a given number of elements will be returned for each execution, and may even return zero elements, but as long as the cursor returned by the command is not 0, the application should not treat the iteration as ending.
However, the number of elements returned by the command always conforms to certain rules. For a large dataset, the incremental iterative command may return up to dozens of elements at a time, while for a dataset that is small enough, it may return all key in one iteration.
COUNT option
For incremental iteration commands that do not guarantee the number of elements returned for each iteration, we can use the COUNT option to adjust the behavior of the command to some extent. The function of the COUNT option is to let the user tell the iteration command how many elements should be returned from the dataset in each iteration. Using the COUNT option is equivalent to a prompt for incremental iteration commands, which in most cases effectively controls the number of values returned.
Note: the COUNT option does not strictly control the number of key returned, it can only be described as a rough constraint. Not every iteration needs to use the same COUNT value, users can change the COUNT value as needed in each iteration, as long as remember to use the cursor returned by the last iteration in the next iteration.
MATCH option
Similar to the KEYS command, the incremental iterative command enables the command to return only the elements that match the given pattern by providing a glob-style pattern parameter, given the MATCH parameter.
The MATCH option does pattern matching for elements during the period of time after the command fetches the element from the dataset and before it returns the element to the client, so if only a small number of elements match the pattern in the iterated dataset, the iterative command may not return any elements in multiple executions.
The following is an example of this:
Redis 127.0.0.1 MATCH 6379 > key:911 0 MATCH * 1131) 1) "key:911" redis 127.0.0.1 redis 6379 > scan 288 MATCH * 1131) "224" 2) (empty list or set) redis 127.0.1 MATCH * 1131) "80" 2) (empty list or set) redis 127.0.1 MATCH > scan 80 MATCH * 111) (176 "2) Empty list or set) redis 127.0.0.1 MATCH * 11 * COUNT 10001) "0" 2) 1) "key:611" 2) "key:711" 3) "key:118" 4) "key:117" 5) "key:311" 6) "key:112" 7) "key:111" 8) "key:110" 9) "key:113" 10) "key:211" 11) "key:411" 12) "key:115" 13) "key:116" 14) "key:114" 15) "key:119" 16) "key:811" 17) "key:511" 18) "key:11" redis 127.0.0.1 key:116 6379 >
As you can see, most of the above iterations do not return any elements. In the last iteration, we forced the command to scan more elements for this iteration by setting the parameter of the COUNT option to 1000, so that the command returned more elements.
Based on the security of SCAN, it is recommended that you use the SCAN command instead of KEYS in the production environment, but note that this command was added after version 2.8.0. If your Redis is lower than this version, you need to upgrade Redis.
The following demonstrates the use of the SCAN command in PHP code:
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.