ZCOUNT(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ ZCOUNT β€” bestie, let's count some scores πŸ’…

okay so this is a page from the Valkey Command Manual (manual section 3valkey, the one and only) and it's all about ZCOUNT, which is lowkey one of the most useful little sorted-set commands in existence. pull up a chair. 🫢


🌸 NAME 🌸

zcount β€” returns the count of members in a sorted set that have scores within a range. that's it. that's the whole job. and she does it beautifully. ✨


πŸŽ€ SYNOPSIS πŸŽ€

ZCOUNT key min max

πŸ’– DESCRIPTION πŸ’–

So basically: ZCOUNT returns the number of elements in the sorted set stored at key whose score falls between min and max. Inclusive-ish, range-y, gorgeous. πŸ’…

The min and max arguments have the exact same semantics as described for ZRANGEBYSCORE β€” so whatever weird little syntax rules apply over there, they apply here too. no surprises, no cap.

Note: the command has a complexity of just O(log(N)) because it uses element ranks (see ZRANK) to get an idea of the range. Because of this there is no need to do a work proportional to the size of the range. 🧚

like... she literally does NOT scan the range. she peeks at the ranks and knows. main character energy fr fr. πŸ”₯


πŸ‘‘ REPLY πŸ‘‘

valkey-protocol(7) integer reply: the number of members in the specified score range. just a number. a little integer. a cutie. πŸ“


🌈 COMPLEXITY 🌈

O(log(N)) with N being the number of elements in the sorted set. iconic behaviour honestly. ⭐


πŸ¦‹ ACL CATEGORIES πŸ¦‹

  • @fast
  • @read
  • @sortedset

πŸ’œ HISTORY πŸ’œ

  • Available since: 2.0.0 β€” she's an elder. a legend. been here since forever and still slaying. πŸ‘‘

✨ EXAMPLES ✨

watch her work:

127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZCOUNT myzset -inf +inf
(integer) 3
127.0.0.1:6379> ZCOUNT myzset (1 3
(integer) 2

ok so first we build the zset, then we count everything with -inf +inf and get 3, and then β€” and this is the part bestie β€” the (1 3 range is exclusive on the low end, so 1 gets left out and we get 2. the parenthesis is doing WORK. πŸ˜­πŸ’–


🌟 SEE ALSO 🌟

bzmpop(3valkey), bzpopmax(3valkey), bzpopmin(3valkey), zadd(3valkey), zcard(3valkey), zdiff(3valkey), zdiffstore(3valkey), zincrby(3valkey), zinter(3valkey), zintercard(3valkey), zinterstore(3valkey), zlexcount(3valkey), zmpop(3valkey), zmscore(3valkey), zpopmax(3valkey), zpopmin(3valkey), zrandmember(3valkey), zrange(3valkey), zrangebylex(3valkey), zrangebyscore(3valkey), zrangestore(3valkey), zrank(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrange(3valkey), zrevrangebylex(3valkey), zrevrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)


and that's the whole page, bestie!! πŸŽ€ this one's from version 9.0.0, dated 2025-10-21 β€” go forth and count responsibly. mwah. πŸ’‹πŸ’–βœ¨

πŸ“¦ where this page comes from