Redis MoreKey problem and Scan command interpretation

Table of contents

MoreKey Discussion

Scan command

Sscan command 

Hscan command

Zscan command


MoreKey Discussion

keys * View all keys in the current library

Executing key * for massive data will cause serious service lag and affect business. Best not to use in real environment. During the manufacturing process, dangerous commands such as keys * / flushdb / flushall are used to prevent accidental deletion and misuse.

 Using key* to view or delete a large amount of data will cause serious time consumption. This command has no offset and limit parameters, and it is to spit out all the keys that meet the conditions at one time. Since redis is single-threaded, all its operations are atomic. , and the keys algorithm is a traversal algorithm with a complexity of O(n). If there are tens of millions of keys in the instance, this command will cause the Redis service to freeze, and all other commands for reading and writing Redis will be delayed or even It will time out and report an error, which may cause a cache avalanche or even a database downtime.

These commands can be disabled through configuration settings, redis.conf in the SECURITY item.

key * cannot be used, what to use?

Use the Scan command. Similar to but not identical to mysql limit, the Scan command is used to iterate over the database keys in the database

Scan command

The Redis Scan command is used to iterate over the database keys in the database.

The SCAN command is a cursor-based iterator. After each call, a new cursor will be returned to the user. The user needs to use this new cursor as the cursor parameter of the SCAN command in the next iteration to continue the previous iteration process. .

SCAN returns an array containing two elements, the first element is the new cursor for the next iteration, and the second element is an array containing all the elements being iterated. If the new cursor returns 0, the iteration is over.

The basic syntax of the redis Scan command is as follows:

SCAN cursor [MATCH pattern] [COUNT count]
  • cursor - the cursor.
  • pattern - the pattern to match.
  • count - optional, used to specify the number of keys returned for each iteration, the default value is 10.
redis 127.0.0.1:6379> scan 0   # 使用 0 作为游标,开始新的迭代
1) "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:6379> scan 17  # 使用的是第一次迭代时返回的游标 17 开始新的迭代
1) "0"
2) 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"

The traversal order of SCAN:
very special, it does not traverse from the zeroth bit of the first-dimensional array to the end, but uses high-order carry addition to traverse. The reason for traversing in such a special way is to avoid duplication and omission of slot traversal when the dictionary is expanding and shrinking. 

Sscan command 

The Redis Sscan command is used to iterate the elements of the key in the collection, and Sscan inherits from scan

SSCAN key cursor [MATCH pattern] [COUNT count]
  • cursor - the cursor.
  • pattern - the pattern to match.
  • count - optional, used to specify the number of keys returned for each iteration, the default value is 10.

Return value: array list.

redis 127.0.0.1:6379> scan 0   # 使用 0 作为游标,开始新的迭代
1) "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:6379> scan 17  # 使用的是第一次迭代时返回的游标 17 开始新的迭代
1) "0"
2) 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

Hscan command

 The Redis HSCAN command is used to iterate over key-value pairs in a hash table.

HSCAN key cursor [MATCH pattern] [COUNT count]
  • cursor - the cursor.
  • pattern - the pattern to match.
  • count - specifies how many elements to return from the dataset, the default value is 10.

Return value: Each returned element is a tuple, and each tuple element is composed of a field (field) and a value (value).

> HMSET sites google "google.com" runoob "runoob.com" weibo "weibo.com" 4 "taobao.com"
OK
> HSCAN sites 0 match "run*"
1) "0"
2) 1) "runoob"
2) "runoob.com"

Zscan command

The Redis Zscan command is used to iterate elements in an ordered collection (including element members and element scores) 

ZSCAN key cursor [MATCH pattern] [COUNT count]
  • cursor - the cursor.
  • pattern - the pattern to match.
  • count - specifies how many elements to return from the dataset, the default value is 10.

Return value: Each returned element is an ordered set element, and an ordered set element consists of a member and a score.

> ZADD site 1 "Google" 2 "Runoob" 3 "Taobao" 4 "Weibo"
(integer) 4
> ZSCAN site 0 match "R*"
1) "0"
2) 1) "Runoob"
2) 2.0

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/132566749