table of Contents
1, keys key commands shortcomings
2, the introduction scan command
5, allowing the midway stop iterating
6, when the end of the iteration
1, keys key commands shortcomings
2, the introduction scan command
3, scan use
- cursor integer user cursor is set to 0, it indicates the start of a new iteration
- regular expression pattern
- count limit
- Default 10
- 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)
- 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
- 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.
- Legal values of the cursor only two:
- At the beginning of a new iteration, the cursor must be 0;
- Iteration of an iterative command returns the value of the cursor before use.
- The next iteration of the cursor
- This iteration result set (there may be empty)
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