scan command

For example, what are the keys starting with key111?

If you use the keys command, execute keys key1111* to find out all at once.

Similarly, if you use the scan command, use scan 0 match key1111* count 20

scan的语法为:SCAN cursor [MATCH pattern] [COUNT count] The default COUNT value is 10.

The SCAN command is a cursor-based iterator. This means that each time the command is called, the cursor returned by the previous call needs to be used as the cursor parameter of this call to continue the previous iteration process.
Here, the scan 0 match key1111* count 20 command is used to complete the query. What is a bit unexpected is that there is no query result at the beginning of the use. This depends on the principle of the scan command.
When scan is traversing keys, 0 means the first time, key1111* means matching according to the pattern starting with key1111, 20 in count 20 does not mean outputting qualified keys, but limiting the number of dictionary slots that the server can traverse in a single time (Approximately equal to).

So, what is called slot data? Is this slot a slot in a Redis cluster? the answer is negative. In fact, the above picture has already given the answer.
If the number of "dictionary slots" mentioned above is the number of slots in the cluster, and you know that the number of slots in the cluster is 16384, then after traversing 16384 slots, all key information must be traversed
. As you can see clearly above, when traversing When the number of dictionary slots is 20000, the cursor still has not completed the traversal result, so this dictionary slot is not equal to the concept of slot in the cluster.
After testing, when scanning, how big the COUNT value can be completely matched to the qualified key is related to the number of keys of the specific object.
If you scan with a count that exceeds the number of keys, you will definitely find it all at once. To all eligible keys, for example, when the number of keys is 10W, traversing 20w dictionary slots at a time will definitely traverse the results.

 

The scan instruction is a series of instructions. In addition to traversing all keys, it can also traverse the specified container set.
zscan traverses the elements of the zset collection,
hscan traverses the elements of the hash dictionary, and
sscan traverses the elements of the set collection.
The first parameter of the SSCAN command, HSCAN command and ZSCAN command is always a database key (a specified key).

Guess you like

Origin blog.csdn.net/qq_32907195/article/details/112920358