简体   繁体   English

Redis质量增量更新

[英]redis mass incrementation updates

I have about 10,000,000 records inside a redis database. 我在redis数据库中大约有10,000,000条记录。 I received a single columned CSV file with about 100,000 strings which correspond to keys in my redis database. 我收到了一个单列CSV文件,其中包含大约100,000个字符串,这些字符串与redis数据库中的键相对应。 For each of these strings inside the CSV I need to increment the value in redis by one. 对于CSV中的每个字符串,我需要将redis中的值增加1。 Normally to increment the INCR command is used, but is there a way I can make this faster than creating a loop that iterates 100,000 times and sends an INCR command one by one to change each key value individually? 通常使用递增INCR命令,但是有什么方法可以比创建一个循环进行100,000次并逐个发送INCR命令以分别更改每个键值的循环更快呢? Is there a more mass way to update? 有更大规模的更新方式吗?

First of all, each redis driver has "Pipeline" to execute batch commands. 首先,每个redis驱动程序都有“ Pipeline”来执行批处理命令。 You dont need to send the incr command one by one but send them together to redis server. 您不需要一一发送incr命令,而是将它们一起发送到redis服务器。

Second, if there are duplicate keys in your 100,000 strings, use "INCRBY" command. 其次,如果您的100,000个字符串中有重复的键,请使用“ INCRBY”命令。 For instance, the doc is "k1,1; k2,2; k1,3", then you can use "INCRBY k1 2" instead of 2 "INCR k1" 例如,文档是“ k1,1; k2,2; k1,3”,那么您可以使用“ INCRBY k1 2”代替2个“ INCR k1”

Note: the following is pure speculation and needs testing to be verified :) 注意:以下是纯粹的推测,需要测试才能验证:)

@Mark_H's answer is textbook (+1) but I have a wild idea that you can test if you want. @Mark_H的答案是教科书(+1),但我有一个很主意的想法,您可以测试是否需要。 Assuming (and that's a big assumption) that your 10M or so keys are serializable and that given the position of a key in the sequence you can derive the relevant key's name (eg if the names are based on a continuous numerical identifier) how about preparing a bitstring and have the set bits indicate an increment operation? 假设(这是一个很大的假设)您的10M左右密钥是可序列化的,并且给定密钥在序列中的位置,则可以派生相关密钥的名称(例如,如果名称基于连续的数字标识符),如何准备一个位串,是否已设置的位指示递增操作?

Such a bit value would be about 1.2MB in size, but the alternative is sending 100K ops (pipelined or not) so this is more network efficient. 这样的比特值大约为1.2MB,但是另一种选择是发送100K op(无论是否流水线),因此网络效率更高。 How about performance? 性能如何? The next part of the idea is to write a small Lua that actually accepts this value and does the INCR for relevant keys. 想法的下一部分是编写一个小的Lua,它实际上接受此值并对相关密钥进行INCR。 I suspect it would perform equally as, if not better. 我怀疑它的性能是否可以甚至更好。

Keep us updated if you try this ;) 如果您尝试这样做,请及时通知我们;)

PS another hidden assumption in my answer is that you're only adding 1's, but that can be resolved by repeating the approach for other numbers/additional keys. PS我的答案中另一个隐藏的假设是,您只添加1,但是可以通过对其他数字/附加键重复此方法来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM