antirez/redis — pull request #1786 by kjmph (2014-05-30 18:45:29+00:00)
Title: ZUNIONSTORE performance change

When attempting to union many keys, ZUNIONSTORE was taking 14s. With these modifications, it is down to ~200ms. However, I guarantee I don't understand how refcnts are used in Redis. The reason I need refcnts is because when aggregating, I use the destination ZSET as the accumulated aggregate. Then, because ZSETs sort on insert, I need to sort the destination. I did so by creating a new ZSET and inserting all the values/scores into it. The method described here is more cache friendly by iterating through each key, rather than scanning forward through all keys for the aggregate.