REDIS 中的 SCAN/HSCAN 命令是否有任何推荐的 COUNT 值? [英] Is there any recommended value of COUNT for SCAN / HSCAN command in REDIS?

查看:363
本文介绍了REDIS 中的 SCAN/HSCAN 命令是否有任何推荐的 COUNT 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经理解了 REDIS SCAN 中 COUNT 的含义.但是,REDIS COUNT 的理想值是多少?

I have understood the meaning of COUNT in the case of REDIS SCAN. But, what is the ideal value for REDIS COUNT ?

推荐答案

默认值为 10.这意味着该命令将带回或多或少 10 个键,如果键在哈希槽中稀疏填充,或者被 MATCH 模式过滤掉,则可能会更少.如果某些键共享一个哈希槽,可能会更多.无论如何,执行的工作与 COUNT 参数成正比.

The default value is 10. It means the command will bring back more or less 10 keys, could be less if the keys are sparsely populated in the hash slots, or filtered out by the MATCH pattern. It could be more if some keys are sharing a hash slot. Anyhow, the work performed is proportional to the COUNT parameter.

Redis 是单线程的.引入 SCAN 的原因之一是允许在不长时间阻塞服务器的情况下遍历所有密钥,一次执行几个步骤.

Redis is single-threaded. One of the reasons SCAN was introduced is to allow going through all the keys without blocking the server for a long time, by going a few steps at a time.

这正是决定什么是好数字的标准.您愿意通过运行 SCAN 命令阻止您的 Redis 服务器多长时间.COUNT 越高,块越长.

And that's precisely the criteria to decide what's a good number. For how long are you willing to block your Redis server by running a SCAN command. The higher the COUNT, the longer the block.

让我们使用 Lua 脚本来了解 COUNT影响.在您的环境中使用它以获取基于您的服务器资源的结果.

Let's use a Lua script to get a sense of the COUNT impact. Use it on your environment to get the results based on your server resources.

Lua 脚本:

local t0 = redis.call('TIME')
local res = redis.call('SCAN', ARGV[1], 'COUNT', ARGV[2])
local t1 = redis.call('TIME')
local micros = (t1[1]-t0[1])*1000000 + t1[2]-t0[2]
table.insert(res,'Time taken: '..micros..' microseconds')
table.insert(res,'T0: '..t0[1]..string.format('%06d', t0[2]))
table.insert(res,'T1: '..t1[1]..string.format('%06d', t1[2]))
return res

这里我们使用 Redis TIME 命令.命令返回:

Here we use Redis TIME command. The command returns:

  • unix 时间(以秒为单位)
  • 微秒

在我的机器上运行了几次,有 100 万个密钥:

A few runs in my machine, with 1 million keys:

COUNT    TIME IN MICROSECONDS
   10            37
  100           257
 1000          1685
10000         14438

请注意,这些时间不包括用于从套接字读取以及缓冲和发送响应的时间.实际时间会更大.一次花费的时间是在 Redis 之外,包括在网络上传播的时间不是你的 Redis 服务器被阻塞的时间.

Note these times don't include the time used to read from the socket and to buffer and send the response. Actual times will be larger. The time it takes once is out of Redis, including time traveling the network is not time your Redis server is blocked though.

这就是我调用 Lua 脚本和结果的方式:

This is how I called the Lua script and the results:

> EVAL "local t0 = redis.call('TIME') \n local res = redis.call('SCAN', ARGV[1], 'COUNT', ARGV[2]) \n local t1 = redis.call('TIME') \n local micros = (t1[1]-t0[1])*1000000 + t1[2]-t0[2] \n table.insert(res,'Time taken: '..micros..' microseconds') \n table.insert(res,'T0: '..t0[1]..string.format('%06d', t0[2])) \n table.insert(res,'T1: '..t1[1]..string.format('%06d', t1[2])) \n return res" 0 0 5
1) "851968"
2) 1) "key:560785"
   2) "key:114611"
   3) "key:970983"
   4) "key:626494"
   5) "key:23865"
3) "Time taken: 36 microseconds"
4) "T0: 1580816056349600"
5) "T1: 1580816056349636"

这篇关于REDIS 中的 SCAN/HSCAN 命令是否有任何推荐的 COUNT 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆