Scan using the Redis commands in

 

Redis has a classic question, in the case of the huge amount of data, do things like look up information Key's meet certain rules, there are two ways here,
one is command keys, simple and crude, as this single-threaded Redis a characteristic, keys command is executed blocking the way, keys is a way to achieve the complexity of traversal is O (n), the more Redis library key, look to achieve greater cost to produce longer blocking time.
Second scan command to find the way to achieve non-blocking key value, in most cases can replace command keys, optional stronger

 
The following written 100,000 key ***: value *** test data format (ps: if a pipline, 1w sum, each sum in the second stage is completed)
# -*- coding: utf-8 -*-
# !/usr/bin/env python3
import redis
import sys
import datetime

def create_testdata(): r = redis.StrictRedis(host='***.***.***.***', port=****, db=0, password='root') counter = 0 with r.pipeline(transaction=False) as p: for i in range(0, 100000): p.set('key' + str(i), "value" + str(i)) counter = counter + 1 if (counter == 10000): p.execute() counter = 0 print("set by pipline loop") if __name__ == "__main__": create_testdata()
 
For example, the query beginning key111 What's key here?

If you use command keys, perform the keys key1111 *, check out all at once.

Similarly, if a scan command, then use scan 0 match key1111 * I count 20

scan的语法为:SCAN cursor [MATCH pattern] [COUNT count] The default COUNT value is 10.

SCAN command is a cursor-based iterators. This means that the command is invoked every time a need to use this as a call to return the cursor cursor parameters of the invocation, in order to continue the iterative process before.
Here the use of scan 0 match key1111 * count 20 command to complete this inquiry, somewhat surprisingly, did not start using a query to the results, the scan command from the principle point of view.
scan when traversing key, 0 represents the first time, key1111 * according to the model represents the beginning of key1111 match, count 20 is not representative of the output of the 20 qualified key, but limited the number of slots dictionary a single pass through the server (approximately equal).

So, what is also called data slot? This slot is not Redis cluster slot? the answer is negative. In fact, the figure has been given the answer.
If it says the number of "dictionary slot" is a cluster of slot, but also know the number of slot cluster is 16384, then after traversing 16,384 slots, it must be able to traverse all the key information
above clearly see that when traversing 20,000 the number of slots dictionary when the cursor is still not completed through the results, so the dictionary does not mean that the concept of slot groove in the cluster.
After testing, the scan time, how to traverse much COUNT value to match fully qualified key, with the key number of specific objects related to,
if the count exceeds the number key to scan, you will definitely find the one-time to all eligible key, such as in the case of a key number of 10W, once traversed 20w dictionaries slots, certainly completely traverse the results come out.

 

scan 指令是一系列指令,除了可以遍历所有的 key 之外,还可以对指定的容器集合进行遍历。
zscan 遍历 zset 集合元素,
hscan 遍历 hash 字典的元素、
sscan 遍历 set 集合的元素。
SSCAN 命令、 HSCAN 命令和 ZSCAN 命令的第一个参数总是一个数据库键(某个指定的key)。

另外,使用redis desktop manager的时候,当刷新某个库的时候,控制台自动不断刷新scan命令,也就知道它在干嘛了

 

 

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

 

Guess you like

Origin www.cnblogs.com/wy123/p/10955153.html