gpt4 book ai didi

redis - loop get vs mget,redis lua是否有性能差异?

转载 作者:行者123 更新时间:2023-12-03 06:42:40 32 4
gpt4 key购买 nike

给定支持lua脚本的单个实例redis。一次调用“mget”和多次调用“get”以检索多个键的值之间在性能上有什么区别吗?

最佳答案

时间复杂度都相同: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"

关于redis - loop get vs mget,redis lua是否有性能差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60221077/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com