Finding values by using partial key name in a Redis Sorted Set

后端 未结 2 842
说谎
说谎 2021-02-10 17:33

I have a sorted set which has the following key name and values :

zrange bargraph:branch:1:category:2:product:4
1) \"76\"
2) \"55\"
3) \"10\"
4) \"84\"
<         


        
2条回答
  •  心在旅途
    2021-02-10 18:06

    As you've mentioned, KEYS is inefficient because the engine performs a linear scan for keys. Unfortunately, there is no wildcard solution such as you are looking for

    Consider using a SET for your product keys per category:

    SADD bargraph:branch:1:category:2 1 2 3 4

    to fetch all set category members do:

    SMEMBERS bargraph:branch:1:category:2

    If you don't care about summing your scores, or have different items per a sorted set, you can do a union of your per product sorted sets like this:

    ZUNIONSTORE bargraph:branch:1:category:2:product:all 4 bargraph:branch:1:category:2:product1 bargraph:branch:1:category:2:product2 bargraph:branch:1:category:2:product3 bargraph:branch:1:category:2:product4

    and now you can zrange bargraph:branch:1:category:2:product:all

    You pipeline the above operations for better performance

提交回复
热议问题