loop get vs mget, 在 Redis Lua 中是否存在性能差异?

假设我们有一个支持 Lua 脚本的 Redis 实例。在检索多个键的值时,一次性调用 'mget' 和多次调用 'get' 之间是否存在性能差异?

点赞
用户10915131
用户10915131

在某些情况下会有所不同。

MGET key [key ...] 时间复杂度:O(N),其中 N 是要检索的键的数量。

GET key 时间复杂度:O(1)

根据时间复杂度,GET 更有效率。

然而,在将单个命令传递给 Redis 和多次传递之间存在差异。

如果您不使用池,则差异会更大。

当然,我建议根据要处理的数据类型来使用它。

您可以决定是否更有效率地以 Maps 方式管理数据。

2020-02-14 06:53:53
用户7017466
用户7017466

就时间复杂度而言,两种方法的复杂度相同: O(N) = N*O(1)

但是每个命令的处理和将结果解析回 Lua 都会产生开销。所以, MGET 性能更好。

您可以测量它。以下脚本接收一个键列表,一个调用多次 GET,另一个调用 MGET

多次调用 GET

local t0 = redis.call('TIME')
local res = {}
for i = 1,table.getn(KEYS),1 do
    res[i] = redis.call('GET', KEYS[i])
end
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

一次调用 MGET

local t0 = redis.call('TIME')
local res = redis.call('MGET', unpack(KEYS))
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

多次调用 GET 耗时 51 微秒,一次 MGET 耗时 20 微秒:

> EVAL "local t0 = redis.call('TIME') \n local res = {} \n for i = 1,table.getn(KEYS),1 do  \n     res[i] = redis.call('GET', KEYS[i]) \n end \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" 10 key:1 key:2 key:3 key:4 key:5 key:6 key:7 key:8 key:9 key:10
 1) "value:1"
 2) "value:2"
 3) "value:3"
 4) "value:4"
 5) "value:5"
 6) "value:6"
 7) "value:7"
 8) "value:8"
 9) "value:9"
10) "value:10"
11) "Time taken: 51 microseconds"
12) "T0: 1581664542637472"
13) "T1: 1581664542637523"
> EVAL "local t0 = redis.call('TIME') \n local res = redis.call('MGET', unpack(KEYS)) \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" 10 key:1 key:2 key:3 key:4 key:5 key:6 key:7 key:8 key:9 key:10
 1) "value:1"
 2) "value:2"
 3) "value:3"
 4) "value:4"
 5) "value:5"
 6) "value:6"
 7) "value:7"
 8) "value:8"
 9) "value:9"
10) "value:10"
11) "Time taken: 20 microseconds"
12) "T0: 1581664667232092"
13) "T1: 1581664667232112"
2020-02-14 07:24:24