I wrote below a bash script to handle the key and redis value. I have about 45-50 million keys in my Redis. I want to get all the values ββand do some processing. For this, my script below takes 1 hour to process 1 million keys. It takes 50 hours to process 50 million keys, and I don't want to do this. I am new to redis cli - can someone please help me optimize the below script, or it would be very helpful if someone can offer some suggestion.
My Redis Key Value Modifier:
Keys - 123.item.media
Values - 93839,abc,98,829 | 38282,yiw,282,282 | 8922,dux,382,993 |
Keys - 234.item.media
Values - 2122,eww,92,211 | 8332,uei,902,872 | 9039,uns,892,782 |
Keys - 839.item.media
Values - 7822,nkp,77,002 | 7821,mko,999,822 |
Below the script, I pass all my keys and calculate how many records I have for each key. For example, this key (123.item.media) has 3 entries, and this (839.item.media) has two entries.
So, for keys and bove values, the output should be: Total ratings: 8
The same thing that I do for all 50 millionth keys - it takes too much time.
My code is:
#!/bin/sh
cursor=-1
keys=""
recordCount=0
while [ $cursor -ne 0 ];
do
if [ $cursor -eq -1 ]
then
cursor=0
fi
reply=`redis-cli SCAN $cursor MATCH "*" COUNT 100`
cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
keys=${reply#[0-9]*[[:space:]]}
for i in $keys
do
value=$(redis-cli GET $i)
temCount=`echo $value | awk -F\| '{print NF}'`
recordCount=`expr ${temCount} + ${recordCount}`
done
done
echo "Total Count: " $recordCount
Rate your help in advance!