Redis Find command --SCAN

table of Contents

 

1, keys key commands shortcomings

2, the introduction scan command

3, scan use

4, more scan instruction

5, allowing the midway stop iterating

6, when the end of the iteration

7, time complexity


1, keys key commands shortcomings

    Redis provides a simple instructions violence keys used to list all the string that meet specific rules key. very simple command keys, a character string can be simple regular, but two obvious drawbacks:
    1) there is no offset, limit parameters, to meet the conditions of all-time spit key, if there are millions of examples of key conditions are satisfied, it could lead to a string of brush full screen without end;
    2) keys are traversal algorithm algorithm complexity is O (n), if there are more than ten million of the key examples, this directive will lead to Redis service Caton, even causing obstruction, because Redis is single-threaded program, the execution order All instructions, other instructions must be executed before they can continue over the keys until the current instruction.
    So, the production environment is generally shielded keys command.

2, the introduction scan command

    SCAN command is a cursor-based iterators. The meaning here is: command each time you call requires the use of a call to return the cursor as the cursor parameters of this call, in order to continue the iterative process before. When the cursor is set SCAN command parameter is 0, the server will begin a new iteration; and when redis server returns a value of 0 to a user cursor, represented iteration has been completed, this is the only way of determining the end of the iteration, but not by return result sets to determine whether the end of the iteration is empty .
Compared scan keys have the following advantages:
    1) Although the complexity is O (n), but it is a step-wise through the cursor, the thread will not be blocked;
    2) providing a limit parameter controls the maximum number of results returned each time, only indicative limit iterative incremental command (hint), the returned result may be more or less;
    3) The server does not need to save the state of the cursor, the cursor is the only state back to the scan cursor integer client;
------------
scan has its drawbacks:
    1) returned results may overlap, the client needs to repeat, this is very important;
    2) if during traversal data modification, data can not traverse to change is uncertain. That is, if an element is added to the data sets in an iterative process, or is in the iterative process is removed from the data set, then this element may be returned, or may not, this is undefined unknown.
    3) a single result returned is empty does not mean the end of the traverse, but depends on the cursor returned value is zero
 

3, scan use

redis-cli scan [cursor] match [pattern] count [limit]
1, a three parameters:
  1. cursor integer user cursor is set to 0, it indicates the start of a new iteration
  2. regular expression pattern
  3. count limit 
    1. Default 10 
    2. Note that the limit does not limit the number of results returned, but a limited number of slots dictionary a single pass through the server)
    3. Note that not every iteration have to use the same COUNT value. Users can freely according to their needs change COUNT value in each iteration, just remember the last iteration cursor returned the next iteration used inside it
    4. Using the wrong cursor. Use interrupted (broken), negative, out of range or other non-normal cursor to perform incremental iteration and does not cause the server to crash, but it may make command produces undefined behavior.
    5. Legal values ​​of the cursor only two:
      1. At the beginning of a new iteration, the cursor must be 0;
      2. Iteration of an iterative command returns the value of the cursor before use.
2, return value:
  1. The next iteration of the cursor
  2. This iteration result set (there may be empty)
3, scan process:
    The first time through, Cursor value is 0, the returned results follow a first integer value for the next traversal cursor. Redis been traversed to return the cursor is ending 0:00.
    0 as the cursor begin a new iteration, always call the SCAN command until the command to return the cursor 0, we call this process a complete traversal.
 

4, scan instruction instances:

$ redis-cli scan 0 match key99* count 1000
1) "13912"
2)  1) "key997"
    2) "key9906"
    3) "key9957"
    4) "key9902"
    5) "key9971"
    6) "key9935"
    7) "key9958"
    8) "key9928"
    9) "key9931"
   10) "key9961"
   11) "key9948"
   12) "key9965"
   13) "key9937"
   
$ redis-cli scan 13912 match key99* count 1000
1) "5292"
2)  1) "key996"
    2) "key9960"
    3) "key9973"
    4) "key9978"
    5) "key9927"
    6) "key995"
    7) "key9992"
    
   从上面的过程可以看到虽然设置的limit是1000,但是返回的结果只有 10 个左右。这是因为因为这个 limit 不是限定返回结果的数量,而是限定服务器单次遍历的字典槽位数量(约等于)。所以如果将limit 设置为 10,你会发现返回结果是空的,但是游标值不为零,意味着遍历还没结束。

如果将limit设置为10,例如下:
$ redis-cli scan 0 match key99* count 10
1) "15360"
2) (empty list or set)

$ redis-cli scan 15360 match key99* count 10
1) "2304"
2) (empty list or set)

4, more scan instruction

scan instruction is a series of instructions, in addition to traverse all the key, but also can be set to traverse the specified container. '

  • SCAN command for database key iterations in the current database,
  • zscan zset traverse the collection of elements,
  • hscan traversal key hash dictionary right,
  • sscan traverse the elements set collection.

    Note, SSCAN command, HSCAN command and the first parameter ZSCAN command is always a key database. SCAN command and you do not need to provide any database keys in the first argument - because it is all iterations of the database keys in the current database.

5, allowing the midway stop iterating

    Because all state iterations are stored in the cursor inside the server without the iterative save any state, so the client can stop in the middle of an iteration, the server without any notice. Even if there is any number of iterations stops in the middle, it will not cause any problems.

6, when the end of the iteration

    When the cursor is 0 redis server returns to the user, it indicates the iteration has been completed, this is the only way of determining the end of the iteration, but can not return a result set is empty is determined whether to end the iteration.

    Meanwhile incremental iterative algorithm used command is only guaranteed when the data set size bounded (bounded), the iteration will stop, in other words, if it is continued to grow the size of the data set, then iteration, incremental iterative command may never be able to complete a full iteration.

7, time complexity

    Each execution of complexity O (1), a complete data set of iteration complexity is O (N), where N is the number of elements in the data set.

 

8. References

http://jinguoxing.github.io/redis/2018/09/04/redis-scan/

http://doc.redisfans.com/key/scan.html

 

Published 48 original articles · won praise 35 · views 80000 +

Guess you like

Origin blog.csdn.net/kqZhu/article/details/104408644