I wrote below bash script to process redis key and value. I've around 45-50 millions of keys in my Redis. I want to retrieve all values and do some processing. To do that my below script is taking 1 hour to process 1 millions of key. In order to process 50 million key it will take 50 hrs which I don't want to do that. I'm new to redis cli - can someone please help me to optimize the below script or it would be really greatful if someone can provide some suggestion.
My Redis key-value pattern:
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 |
In below script I'm passing all my keys and calculating how much record I have for each key. For example - this key (123.item.media) has 3 records and this one (839.item.media) has two records.
So for bove keys and values, the output should be: Total Count: 8
Same way I'm doing for all 50 millions keys - which is taking too much time.
My code:
#!/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`
#echo $reply
cursor=`expr "$reply" : '\([0-9]*[0-9 ]\)'`
keys=${reply#[0-9]*[[:space:]]}
for i in $keys
do
#echo $i
#echo $keys
value=$(redis-cli GET $i)
temCount=`echo $value | awk -F\| '{print NF}'`
#echo $temCount
recordCount=`expr ${temCount} + ${recordCount}`
done
done
echo "Total Count: " $recordCount
Appreciate your help in advance!