0

I have many index in Redis who store key name associated with their timestamp (it's a zset) imagine I have 5 index :

  • Apple -> 1 , 2 , 3
  • Red -> 1, 2
  • green -> 3
  • big -> 3 , 2
  • small -> 1

I want to know how many Red Apple are big (I can accept a little error margin if it's necessary) the first approach is to intersect apple and red indexes then search 1 and 2 in big but this is resources hungry for big index and if i want to know this for many other index imagine I have 100 index containing 1000 keys, it's impossible in term of speed

second approach are to store for each index a "chain" of index for example when I add a new key, i have access to my key characteristic so i know it's a red small apple, when i add it in Apple i store this information in Apple:Chain = {small -> 1, red -> 1} and then when I have others keys , either I increment existing key or I create a new one, but when i want to know how many red apple are big my chain don't work anymore, because i don't have capability to intersect chain, or i must intersect apple and red then make a chain of the intersect but it's even worst in term of speed compared to the first approach

third approach seem to be approximation of the result, how can i approximatively know how many red apple are big with a reasonable margin of error ? i see Redis have HyperLogLog can i use it for this purpose ?

and finally if you see another approach tell me, even if i had to store another data structure or change the existing one , I'm open to all suggestion

shilovk
  • 11,718
  • 17
  • 75
  • 74
Artha77
  • 31
  • 1

1 Answers1

1

A proper answer would require more information about your data - the number of zsets and the number of keys that they index.

A possibly more efficient, in terms of RAM and CPU, index would be a bitmap per each zset. Use a bit per key to indicate belongingness and call BITOP to perform a logical AND (intersect) between all.

Alternatively, you may want to look at http://redisearch.io, a "blazing fast" search engine built as a Redis module that enables this (and much more) type of searches.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117