Redis commands use the Scan

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 3
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 instruction is a series of instructions, in addition to traverse all the key, but also can be set to traverse the specified container.
zscan zset traverse the collection of elements,
HSCAN traverse the elements of hash dictionary,
SSCAN traversal of the elements set collection.
SSCAN command, HSCAN command and the first parameter ZSCAN command is always a key database (a specified key).

In addition, when using redis desktop manager, when refreshing a library, and constantly refresh the console automatically scan command, it will know why the

Guess you like

Origin www.linuxidc.com/Linux/2019-06/158913.htm