Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
loop get vs mget,redis lua的性能有什么不同吗?_Redis_Lua - Fatal编程技术网

loop get vs mget,redis lua的性能有什么不同吗?

loop get vs mget,redis lua的性能有什么不同吗?,redis,lua,Redis,Lua,给定一个支持lua脚本的redis实例。一次调用“mget”和多次调用“get”来检索多个键的值之间是否存在性能差异?在某些情况下会有所不同 MGET key[key…]时间复杂度:O(N),其中N是要检索的密钥数。 获取密钥的时间复杂度:O(1) 根据时间复杂度,GET的效率更高。 但是,向redis传递单个命令与多次传递命令之间存在差异。 如果您不使用pool,差异会更大 当然,我建议您根据需要处理的数据类型使用它。 您可以决定是否更有效地管理地图中的数据。从时间复杂性的角度来看,两者的结果

给定一个支持lua脚本的redis实例。一次调用“mget”和多次调用“get”来检索多个键的值之间是否存在性能差异?

在某些情况下会有所不同

MGET key[key…]时间复杂度:O(N),其中N是要检索的密钥数。
获取密钥的时间复杂度:O(1)

根据时间复杂度,GET的效率更高。
但是,向redis传递单个命令与多次传递命令之间存在差异。
如果您不使用pool,差异会更大

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

您可以决定是否更有效地管理地图中的数据。

从时间复杂性的角度来看,两者的结果相同:
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"