SCAN

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Look up keys in batches

Usage: scan cursor.
KEYS scans and returns all keys at once, while SCAN returns about 10 keys at a time.
When there are many keys, KEYS takes a long time and cannot process other commands in the meantime. SCAN was introduced as an alternative.
If there are many keys, specify the next cursor and look them up repeatedly. When all keys have been returned, the next cursor is 0.
SSCAN, ZSCAN, and HSCAN look up members of a key, while SCAN looks up all keys in the DB.

Example

Command>scan 0
Result>next cursor -> 1024
0) key001
1) key002
2) key005
...
11) key021
Command>scan 1024
Result>next cursor -> 18944
0) key031
1) key022
2) key035
...
9) key151


Using the MATCH key_pattern option

Usage: scan cursor MATCH pattern.
Looks up only keys that match the specified key_pattern.
This is a glob-style pattern.

Example

Command>scan 0 match key*
Result>next cursor -> 1024
0) key001
1) key002
2) key005
...
11) key021


Using the COUNT count option

Usage: scan cursor COUNT 10.
The number of keys returned is not always exactly the specified count. Redis adjusts the count considering processing time.

Example

Command>scan 0 count 10
Result>next cursor -> 1024
0) key001
1) key002
2) key005
...
11) key021


Using the TYPE type option

Usage: scan cursor TYPE string.
If TYPE is specified, only keys of that type are returned.

Example

Command>scan 0 type string
Result>next cursor -> 1024
0) key001
1) key002
2) key005
...
11) key021


Syntax

SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]

  • Available since version 2.8.0.
    The TYPE option is available since Redis 6.0.0.
  • Time complexity is O(N).
Related commands KEYS, SSCAN, ZSCAN, HSCAN
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< RM SCAN SORT >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.