Mastering Redis
上QQ阅读APP看书,第一时间看更新

Advanced sorted set operations

Similarly to sets, sorted sets in Redis support the set operations of union and intersection, although the time complexity of these operations for sorted sets is worse than for sets. Another problem with the sorted set operations is that when using a Redis cluster, union and intersection operations can only be used when the sorted set keys have been sharded to the same hash slot and run on the same node. The ZINTERSTORE Redis command has a time complexity of O(nk)+O(mlog(m)), where n is the size of the smallest sorted set; k, the total number of sorted sets being intersected, and m, the number of elements in the resulting final sorted set. Likewise, for the ZUNIONSTORE command, the time complexity is O(n)+O(M log(M)) with n being the total size of all the sorted sets and m being the total number of elements in the final sorted set. Given the characteristics of sorted sets, the additional time required for these two set operations may be an acceptable trade-off. It is good to keep in mind this difference in performance between large sets and sorted sets irrespective of whether the data and your requirements require ordering or not.