loop get vs mget, 在 Redis Lua 中是否存在性能差异?
2020-2-14 6:21:46
收藏:0
阅读:152
评论:2
假设我们有一个支持 Lua 脚本的 Redis 实例。在检索多个键的值时,一次性调用 'mget' 和多次调用 'get' 之间是否存在性能差异?
点赞
用户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
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

在某些情况下会有所不同。
MGET key [key ...] 时间复杂度:O(N),其中 N 是要检索的键的数量。
GET key 时间复杂度:O(1)
根据时间复杂度,GET 更有效率。
然而,在将单个命令传递给 Redis 和多次传递之间存在差异。
如果您不使用池,则差异会更大。
当然,我建议根据要处理的数据类型来使用它。
您可以决定是否更有效率地以 Maps 方式管理数据。