Acts similarly to SDIFF, but on sorted sets. ZDIFFSTORE by default ignores scores when comparing values; pass the WITHSCORES option to only exclude when both the value and score match. Any weights passed are applied to the resulting set similarly to ZUNION/INTERSTORE, and are applied before testing WITHSCORES.
The set operations all happen on normal sets (which have the sdiff command) and their results can all be cached because they don't depend on object scores.
There is one rather important assumption I am making for this model to make any sense, and that is that redis is actually able to achieve efficiency gains when intersecting a sorted set and a smaller unsorted set compared with an ad-hoc sort of the unsorted set.
It would be very useful if we could do zset operations that preserve only the scores of the first zset and do not re-sort the results
As far as I can tell, ZDIFFSTORE won't significantly improve performance over SDIFFSTORE and then SORT until the zset skip list insertion algorithm is modified to make appending an element on the rightmost extreme a constant-time operation.
Your runtime is incorrect for the definition of N and M that you have
chosen, I can't think of a definition of N and M that would be correct
for your description.
Algorithmically, there are two (obvious) methods for building ZDIFFSTORE...
1. For each entry in your source zset, check for entries in the other
zsets. If it's not there, add the entry to your destination zset. O(M
log(M) + N*k)
2. Copy your source zset to your destination, iterate over each of
your other zsets, removing entries from your destination as necessary.
O(P log(P) + Q)
M = size of the result zset
N = number of items in the source zset
k = number of zsets to remove items from
P = size of the source zset
Q = number of items in all non-source zsets in total
The first algorithm is what Redis uses for SDIFFSTORE. Which one is
faster depends on M, P, N, k, and Q.
Regards,
- Josiah
>> As far as I can tell, ZDIFFSTORE won't significantly improve performance
>> over SDIFFSTORE and then SORT until the zset skip list insertion algorithm
>> is modified to make appending an element on the rightmost extreme a
>> constant-time operation.
>
> A 27% difference is a significant improvement for us. I'd like to more
> heavily optimize it though, so I may look at implementing non-storing ZSET
> operations as well.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Redis DB" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/redis-db/-/F4BWcTnZpVgJ.
1. For each entry in your source zset, check for entries in the other
zsets. If it's not there, add the entry to your destination zset. O(M
log(M) + N*k)
I must say I'm surprised that using ZDIFFSTORE improves your average performance
The underlying skip list implementation does nothing to improve upon insertion time when elements are inserted in ascending order into an empty zset