hermit.crab
hermit.crab

Reputation: 962

How to efficiently set multiple related keys in redis?

I want to execute the following in a bash script:

num_total_keys=0x1FFFF

for ((i = 0; i <= $num_total_keys; i++))
do
    redis-cli SET some_prefix:$i True > /dev/null
done

When I execute this, however, it takes a really long time. Is there a more efficient way to do this?

Upvotes: 1

Views: 824

Answers (1)

L&#233;a Gris
L&#233;a Gris

Reputation: 19545

Rule of thumb:

If your command accept pipelined data/instructions; do not run your command repeatedly in a shell loop, but build all of it before piping to your command as a single call like this:

#!/usr/bin/env sh

i=131071

while [ $i -ge 0 ]; do
  printf 'SET some_prefix:%d True\n' $i
  i=$((i - 1))
done | redis-cli --pipe

Alternatively using Bash's brace expansion:

printf 'SET some_prefix:%d True\n' {131071..0} | redis-cli --pipe

Upvotes: 2

Related Questions