ZLEXCOUNT(3valkey)
π ZLEXCOUNT β the valkey manual page, but make it make sense π
okay bestie, welcome to the Valkey Command Manual, page ZLEXCOUNT(3valkey). this one's a lil niche but honestly kind of iconic once it clicks. let's get into it π«Ά
β¨ NAME β¨
zlexcount β counts how many members live in a sorted set inside a lexicographical range. yes that's a big word, we'll unpack it, chill.
β¨ SYNOPSIS β¨
ZLEXCOUNT key min max
three little args, one big vibe. π
β¨ DESCRIPTION β¨
so here's the setup: when every single element in a sorted set gets inserted with the same score β which is how you force everything to be ordered lexicographically instead of by score β this command gives you back the number of elements in the sorted set at key whose value sits between min and max.
the min and max arguments mean literally the same thing they mean over in ZRANGEBYLEX. same syntax, same rules, no new lore. iykyk πΈ
note: this command has a complexity of justO(log(N))because it uses elements ranks (seeZRANK) 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. she's efficient, she's not doing extra cardio for nothing. π₯
β¨ REPLY β¨
valkey-protocol(7) Integer reply: the number of members in the specified score range. it's giving one single integer, no drama, no surprises. β
β¨ COMPLEXITY β¨
O(log(N)) with N being the number of elements in the sorted set. so likeβ¦ log of N. tiny. adorable. π
β¨ ACL CATEGORIES β¨
@fastβ‘@readπ@sortedsetπ
β¨ HISTORY β¨
- Available since:
2.8.9β a true elder, been here since forever π
β¨ EXAMPLES β¨
watch her work:
127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5
127.0.0.1:6379> ZADD myzset 0 f 0 g
(integer) 2
127.0.0.1:6379> ZLEXCOUNT myzset - +
(integer) 7
127.0.0.1:6379> ZLEXCOUNT myzset [b [f
(integer) 5
ok so first we stack myzset with a b c d e all at score 0 (got 5 back), then add f and g, also at 0 (got 2 back, total 7, slay). then ZLEXCOUNT myzset - + asks for everything from the very bottom to the very top and, correct, it's 7 β the whole squad. π then ZLEXCOUNT myzset [b [f asks for the range starting at inclusive [b through inclusive [f, and that's 5. inclusive brackets, bestie, the [ matters. no cap. π
β¨ SEE ALSO β¨
the whole friend group, go say hi:
bzmpop(3valkey)bzpopmax(3valkey)bzpopmin(3valkey)zadd(3valkey)zcard(3valkey)zcount(3valkey)zdiff(3valkey)zdiffstore(3valkey)zincrby(3valkey)zinter(3valkey)zintercard(3valkey)zinterstore(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!! π go count some lexicographical ranges, you're gonna eat. this was Valkey 9.0.0, dated 2025-10-21 β mwah, love u, bye π¦ππ
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms