Home  >  Article  >  Database  >  Detailed explanation of Redis key traversal and database management

Detailed explanation of Redis key traversal and database management

WBOY
WBOYforward
2022-10-10 17:23:481581browse

This article brings you relevant knowledge about Redis, which mainly introduces the relevant content about traversal keys and database management. Let’s take a look at it together. I hope it will be helpful to everyone.

Detailed explanation of Redis key traversal and database management

Recommended learning: Redis video tutorial

1 Traversing keys

##1.1 Full volume Traversing keys

Sometimes we need to fully traverse all keys, then we need to use the keys pattern command, and this command supports pattern matching

127.0.0.1:6379> mset name luke neme josh
OK

If you want To traverse all keys, you can use the command keys *

127.0.0.1:6379> keys *
1) "name"
2) "neme"

pattern uses glob-style wildcards, where:

  • * represents any character

  • ? Represents a character

  • [] represents matching part of the characters, for example [a,b] means matching two characters a and b, [1-10] means matching any number from 1 to 10

  • \x means escaping. When you need to match the * character, you need to escape it.

We can do the following:

127.0.0.1:6379> keys n[a,e]me
1) "name"
2) "neme"

You can also operate like this

127.0.0.1:6379> keys n?me
1) "name"
2) "neme"

But when there are a large number of keys in the redis database, keys will block redis.

What should we do if we need to traverse keys?

Generally our production environment is multi-node, then we can find a redis slave node that does not provide external services to traverse the data, but if the amount of data is large, it will still block redis, but for the slave node, it is just Affected master-slave replication.

If you are sure that there are not many keys on redis, you can execute it directly.

1.2 Progressive traversal

Progressive traversal is to traverse part of the key each time, then return, and continue to traverse the subsequent data the next time. In this way, all data can be traversed without blocking the redis service.

scan cursor [MATCH pattern] [COUNT count]

The parameters are explained as follows:

  • cursor is a required parameter. It is a cursor, indicating where the traversal has been. Next time, it will start from this cursor. , if 0 is returned, it means that the traversal is completed.

  • MATCH pattern is an optional parameter, which is the same as the pattern of keys

  • COUNT count indicates how many keys to traverse, the default is 10 , can be increased according to the actual situation

  • 127.0.0.1:6379> mset a 1 b 1 c 1 d 1 e 1 f 1 g 1 h 1 i 1 g 1 k 1 l 1 m 1 n 1 o 1 p 1 q 1 r 1 s 1 t 1 u 1 v 1 w 1 x 1 y 1 z 1
    OK
We use scan to traverse, the first execution returns the following:

127.0.0.1:6379> scan 0
1) "1"
2)  1) "l"
    2) "f"
    3) "k"
    4) "y"
    5) "c"
    6) "e"
    7) "w"
    8) "d"
    9) "b"
   10) "o"
   11) "q"

The second time uses the 1 returned by the first time When traversing, you can traverse to 10 keys

127.0.0.1:6379> scan 1
1) "23"
2)  1) "v"
    2) "u"
    3) "z"
    4) "g"
    5) "n"
    6) "s"
    7) "i"
    8) "a"
    9) "r"
   10) "t"

The third time you use the 23 returned for the second time to traverse. When the return is 0, it means that the traversal is completed

127.0.0.1:6379> scan 23
1) "0"
2) 1) "x"
   2) "h"
   3) "m"
   4) "p"

At the same time, it also There are hscan for hash types, sscan for set types, and zscan for ordered sets. The usage methods are the same as scan

2 Database management

There are several other redis A command for database operations: dbsize, select, flushdb/flushall

2.1 Switch database, select

select dbIndexdbIndex is the corresponding database serial number , there are 16 databases in the default redis configuration, and you can switch to the database number you select.

For example, set a key in the default database No. 0

127.0.0.1:6379> set name luke
OK

Then we switch to database No. 1 to obtain the key, but it cannot be obtained, indicating that there are differences between databases in a redis service Not interoperable.

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
(nil)

So can it be used as multiple redis? Of course not. Although there are more libraries, since redis is single-threaded, it is actually still a CPU. If a command in a database is executed very slowly, other libraries will also be affected. Therefore, in this case, if it is blocked by other libraries, For developers using a certain library, it can be difficult to analyze what the problem is.

2.2 flushall/flushdb

The difference between flushall and flushdb is that flushall will clear all data in all libraries, while flushdb will only clear the current database.

This is easy to understand, so we won’t give examples. However, it should be noted that these two commands will clear all data, and the consequences of misoperation will be disastrous. And when there are too many keys, redis will also be blocked, so you must be careful when using these two commands.

Recommended learning:

Redis video tutorial

The above is the detailed content of Detailed explanation of Redis key traversal and database management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete