Use Scan command Redis of: searching large amounts of data

  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,

  First, command keys, simple and crude, because the single-threaded Redis this feature, command keys are blocking the way of execution, based on the complexity of the keys to traverse the way to achieve is O (n), the more Redis library key, Find realize 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='***')

  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 syntax is:

  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 no. In fact, the figure has been given the answer.

  If it says the number of "dictionary slot" is a cluster slot, and know the number of slot cluster is 16384 (RedisCluster design 16384 slot), then after traversing 16,384 slots, it must be able to traverse out of all the key information,

  Above clearly see that when the number of 20,000 traversed dictionary slot 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

  If the count exceeds the number key to scan, you will definitely find the one-time all qualified 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 traverse 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

  It features scan command:  Zhengzhou good gynecological hospital http://www.kd0371.com/

  1, although the complexity is O (n), but it is a step-wise through the cursor, the thread will not be blocked;

  2, to provide limit parameter controls the maximum number of results returned each time, only a hint limit, the returned results may be more or less;

  3, like keys, it also provides a pattern matching;

  4, the server cursor does not need to save the state, the only state of the cursor is to scan the cursor back to the client an integer;

  5, returned results may overlap, the client needs to repeat, this is very important;

  6, if during traversal data modification, data can not traverse to change is uncertain;

  7, a single result returned is empty does not mean the end of the traverse, but depends on the cursor returned value is zero

Guess you like

Origin www.cnblogs.com/gnz49/p/12620380.html