ZINTER(3valkey)
π ZINTER β bestie's guide to sorted set intersection π
okay so this is a page from the valkey command manual (manual name: valkey, section 3, iykyk) and it's literally all about smashing multiple sorted sets together and keeping only the members they ALL agree on. no cap. π«Ά
β¨ NAME β¨
zinter β returns the intersect of multiple sorted sets. that's it, that's the whole vibe. intersection girlie. πΈ
π SYNOPSIS π
ZINTER numkeys key [key...] [WEIGHTS weight [weight...]] [AGGREGATE
<SUM | MIN | MAX>] [WITHSCORES]
so you tell it how many keys you're bringing to the party (numkeys), then you list the keys, and then optionally you can do the WEIGHTS thing, the AGGREGATE thing, and the WITHSCORES thing. main character energy honestly. π
π¦ DESCRIPTION π¦
This command is similar to ZINTERSTORE, but instead of storing the resulting sorted set, it is returned to the client.
bestie listen.ZINTERSTOREis the one that saves the result somewhere.ZINTERis the one that just hands it straight to you like a little gift. no storing, no commitment, just vibes. π
For a description of the WEIGHTS and AGGREGATE options, see ZUNIONSTORE.
fr fr, go readZUNIONSTOREif you wanna know howWEIGHTSandAGGREGATEwork β this page is not gonna re-explain them, it's giving "go ask my sister". π
π REPLY π
valkey-protocol(7)Array reply: the result of the intersection including, optionally, scores when theWITHSCORESoption is used.
so you get an array back, and if you passed WITHSCORES you also get the scores snuck in there. iconic behaviour honestly. β¨
π COMPLEXITY π
O(NK)+O(Mlog(M)) worst case with N being the smallest input sorted set, K being the number of input sorted sets and M being the number of elements in the resulting sorted set.
the way that it'sO(NK)+O(Mlog(M))in the worst caseβ¦ not me memorizing that.Nis the smallest input set,Kis how many sets you threw in,Mis how many elements survive the intersection. periodt. π
π ACL CATEGORIES π
@read @slow @sortedset
yes bestie it's read-only, yes it's slow (sorry), yes it lives in the sorted set category. the trifecta. π
β HISTORY β
- Available since:
6.2.0
she's been around since 6.2.0, so she's seen things. πΈ
π EXAMPLES π
watch me cook π§π³:
127.0.0.1:6379> ZADD zset1 1 "one"
(integer) 1
127.0.0.1:6379> ZADD zset1 2 "two"
(integer) 1
127.0.0.1:6379> ZADD zset2 1 "one"
(integer) 1
127.0.0.1:6379> ZADD zset2 2 "two"
(integer) 1
127.0.0.1:6379> ZADD zset2 3 "three"
(integer) 1
127.0.0.1:6379> ZINTER 2 zset1 zset2
1) "one"
2) "two"
127.0.0.1:6379> ZINTER 2 zset1 zset2 WITHSCORES
1) "one"
2) "2"
3) "4"
okay wait let me walk you through it because this is actually so satisfying π
zset1gets"one"at score1and"two"at score2. cute.zset2gets"one"at1,"two"at2, and"three"at3. also cute.ZINTER 2 zset1 zset2β you get"one"and"two"."three"got left out because it's not in both. sorry"three", it's giving exclusion. πZINTER 2 zset1 zset2 WITHSCORESβ now you also get the scores, and the default aggregation (SUM) adds them together:"one"is1+1=2and"two"is2+2=4. ate. π₯
π«Ά SEE ALSO π«Ά
go be nosy, all of these are right there:
bzmpop(3valkey)bzpopmax(3valkey)bzpopmin(3valkey)zadd(3valkey)zcard(3valkey)zcount(3valkey)zdiff(3valkey)zdiffstore(3valkey)zincrby(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 valkey version 9.0.0, dated 2025-10-21, so if you're on something olderβ¦ not me judging but like. upgrade. π§ stay intersecting, slay, drink water, 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