redis replaces the keys instruction of with the scan instruction

  • 2020-05-27 07:33:20
  • OfStack

As we all know, the larger the number of key in redis, the slower the keys command will execute, and most importantly, it will block the server, which is a disaster for the single-threaded redis. Finally, the alternative command scan has been found.

SCAN cursor [MATCH pattern] [COUNT count]

The SCAN command and its associated SSCAN, HSCAN, and ZSCAN commands are used to incrementally iterate over (incrementally iterate) 1 set of elements (a collection of elements) :

The SCAN command is used to iterate over the database keys in the current database.

The SSCAN command is used to iterate over the elements in the collection key.

The HSCAN command is used to iterate over the key-value pairs in the hash key.

The ZSCAN command is used to iterate over elements in an ordered collection, including element members and element scores.

Listed above four commands support incremental iteration, they perform every time returns only a small number of elements, so these commands can be used in the production environment, and there will be no problems - like KEYS command, SMEMBERS command when KEYS command is used to deal with a large database, or SMEMBERS command is used to deal with a large collection of key, they might block the server for several seconds.

However, incremental iteration command was not without his faults: for example, using SMEMBERS commands can return currently contains all the elements of the set key, but for SCAN command, this kind of incremental iteration for key in incremental iterative process, key may be modified, so the incremental iteration command to be returned only element provides limited warranty (offer limited guarantees about the returned elements).

Because the four commands SCAN, SSCAN, HSCAN, and ZSCAN all work very similarly, this document will 1 and introduce the four commands, but remember:

The first parameter of the SSCAN, HSCAN, and ZSCAN commands is always a database key.

The SCAN command, on the other hand, does not need to provide any database keys on the first argument -- it iterates over all the database keys in the current database.

scan 0 returns 10 pieces of data by default.

127.0.0.1:6379 > scan 0

1) "81920"
2) 1) "CMD:1000004739:4"
2) "CMD:1000010475:2"
3) "CMD:380071400001208:766"
4) "CMD:1000006866:LIST"
5) "CMD:380071400001208:20415"
6) "CMD:380071400001231:21530"
7) "CMD:380071400001208:21780"
8) "CMD:7485630165:LIST"
9) "CMD:1000001545:2"
10) "CMD:380071400001231:4387"

The amount of data returned can be specified with the count parameter:

127.0.0.1:6379 > scan 0 count 100

1) "104448"
2) 1) "CMD:1000004739:4"
2) "CMD:1000010475:2"
3) "CMD:380071400001208:766"
4) "CMD:1000006866:LIST"
5) "CMD:380071400001208:20415"
6) "CMD:380071400001231:21530"
7) "CMD:380071400001208:21780"
8) "CMD:7485630165:LIST"
9) "CMD:1000001545:2"
10) "CMD:380071400001231:4387"
......
94) "CMD:201610200062:6"
95) "CMD:VF3748211006:3"
96) "CMD:1000009121:4"
97) "CMD:380071400001231:6563"
98) "CMD:1000010252:ID"
99) "CMD:1000005261:5"
100) "SERVER:45568_0"

Use the match parameter to match the pattern:

127.0.0.1:6379 > scan 0 match CMD* count 100

1) "104448"
2) 1) "CMD:1000004739:4"
2) "CMD:1000010475:2"
3) "CMD:380071400001208:766"
4) "CMD:1000006866:LIST"
5) "CMD:380071400001208:20415"
6) "CMD:380071400001231:21530"
7) "CMD:380071400001208:21780"
8) "CMD:7485630165:LIST"
9) "CMD:1000001545:2"
10) "CMD:380071400001231:4387"
......
86) "CMD:201610200062:6"
87) "CMD:VF3748211006:3"
88) "CMD:1000009121:4"
89) "CMD:380071400001231:6563"
90) "CMD:1000010252:ID"
91) "CMD:1000005261:5"

The most important thing is that scan doesn't block the server. It's convenient to use the current network environment.


Related articles: