gpt4 book ai didi

Redis 批量插入 : protocol vs inline commands

转载 作者:IT王子 更新时间:2023-10-29 06:13:20 25 4
gpt4 key购买 nike

对于我的任务,我需要尽快将大量数据加载到 Redis 中。看起来这篇文章对我的情况是正确的:https://redis.io/topics/mass-insert

本文首先给出了一个在 redis-cli 中使用多个内联 SET 命令的示例。然后他们继续生成 Redis 协议(protocol)并再次将其与 redis-cli 一起使用。他们没有解释使用 Redis 协议(protocol)的原因或好处。

Redis 协议(protocol)的使用有点困难,它会产生更多的流量。我想知道,使用 Redis 协议(protocol)而不是简单的单行命令的原因是什么?可能尽管数据更大,但 Redis 解析它更容易(也更快)?

最佳答案

好点。

Only a small percentage of clients support non-blocking I/O, and not all the clients are able to parse the replies in an efficient way in order to maximize throughput. For all this reasons the preferred way to mass import data into Redis is to generate a text file containing the Redis protocol, in raw format, in order to call the commands needed to insert the required data.

我的理解是当你直接使用 Redis 协议(protocol)时,你模拟了一个客户端,这将受益于突出显示的点。

根据您提供的文档,我尝试了这些脚本:

测试.rb

def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
(0...100000).each{|n|
STDOUT.write(gen_redis_proto("SET","Key#{n}","Value#{n}"))
}

test_no_protocol.rb

(0...100000).each{|n|
STDOUT.write("SET Key#{n} Value#{n}\r\n")
}

  • ruby test.rb > 100k_prot.txt
  • ruby test_no_protocol.rb > 100k_no_prot.txt
  • 时间猫 100k.txt | redis-cli --pipe
  • 时间猫 100k_no_prot.txt | redis-cli --pipe

我得到了这些结果:

teixeira: ~/stackoverflow $ time cat 100k.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 100000

real 0m0.168s
user 0m0.025s
sys 0m0.015s
(5 arquivo(s), 6,6Mb)

teixeira: ~/stackoverflow $ time cat 100k_no_prot.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 100000

real 0m0.433s
user 0m0.026s
sys 0m0.012s

关于Redis 批量插入 : protocol vs inline commands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441916/

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