ZLEXCOUNT(3valkey)

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

πŸ’– 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 just O(log(N)) because it uses elements 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. 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:


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