"global" calculations for sorted sets

With a sorted set, is there an easy way to calculate β€œglobal” values, such as average, median, maximum, minimum, etc. all the points in the set, or do I always need to extract estimates and do the calculations myself (which is a little painful)?

+4
source share
3 answers

You can do it all with ZCARD and ZRANGE (subject to course).

  • Min: ZRANGE at index 0
  • Max. ZRANGE at maximum index (ZCARD value)
  • Median: ZRANGE on the average index (ZCARD / 2)
  • Average value: you can save the total number of all points in another counter, and then divide it by the ZCARD value
+3
source

There is no easy way to do this. Aggregation teams are not many.

Edit: if what you need to do, maybe a sorted set (or even redis) is not the way to go. Can you tell us more about your case?

+1
source

You can add another key with the sum of all the points in a sorted set. You will need to update it manually (in the case of multi / exec, probably) on each insert / update / delete of the sorted set. Then you can get the value and divide it by ZCARD to get the average value.

0
source

Source: https://habr.com/ru/post/1381094/


All Articles