KEYS(3valkey)
β¨ KEYS (from the valkey command manual, bestie) β¨
okay so real talk, this is keys β the command that's like "show me EVERY key that matches this pattern" π
she's powerful, she's simple, and she's lowkey a menace if you use her wrong. let's get into it π«Ά
πΈ SYNOPSIS πΈ
KEYS pattern
π DESCRIPTION π
Returns all the keys matching pattern. that's it, that's the whole job β¨
so about speed: the time complexity here is O(N), but the constant times are fairly low. like, for real β Valkey running on an entry level laptop can scan a 1 million key database in 40 milliseconds. iconic behavior honestly π
β οΈ warning bestie: considerKEYSa command that should only be used in production environments with extreme care. she may ruin performance when executed against large databases. this command is intended for debugging and special operations, such as changing your keyspace layout. don't useKEYSin your regular application code. if you're looking for a way to find keys in a subset of your keyspace, consider usingvalkey-scan(7)SCANor sets instead. periodt π
supported glob-style patterns, let's go:
h?llomatcheshello,halloandhxlloh*llomatcheshlloandheeeelloh[ae]llomatcheshelloandhallo, but nothilloh[^e]llomatcheshallo,hbllo, ... but nothelloh[a-b]llomatcheshalloandhbllo
use \ to escape special characters if you want to match them verbatim π
and now the cluster tea β β when using valkey-cluster-tutorial(7) Valkey Cluster, the search is optimized for patterns that imply a single slot. if a pattern can only match keys of one slot, Valkey only iterates over keys in that slot, rather than the whole database, when searching for keys matching the pattern. for example, with the pattern {a}h*llo, Valkey would only try to match it with the keys in slot 15495, which hash tag {a} implies. to use pattern with hash tag, see valkey-cluster-spec(7) Hash tags in the Cluster specification for more information. iykyk β¨
π¦ REPLY π¦
valkey-protocol(7) Array reply: a list of keys matching pattern. so you get the whole lineup, bestie π
π₯ COMPLEXITY π₯
O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length. π
π ACL CATEGORIES π
@dangerous @keyspace @read @slow
(yeah she's tagged @dangerous for a reason, don't say i didn't warn you π)
π HISTORY π
- Available since: 1.0.0 β she's been here since day one, a true OG π
β EXAMPLES β
127.0.0.1:6379> MSET firstname Jack lastname Stuntman age 35
OK
127.0.0.1:6379> KEYS *name*
1) "lastname"
2) "firstname"
127.0.0.1:6379> KEYS a??
1) "age"
127.0.0.1:6379> KEYS *
1) "age"
2) "lastname"
3) "firstname"
look at her go π§ matching everything, no notes.
π SEE ALSO π
copy(3valkey),del(3valkey),dump(3valkey),exists(3valkey),expire(3valkey),expireat(3valkey),expiretime(3valkey),migrate(3valkey),move(3valkey),object(3valkey),object-encoding(3valkey),object-freq(3valkey),object-help(3valkey),object-idletime(3valkey),object-refcount(3valkey),persist(3valkey),pexpire(3valkey),pexpireat(3valkey),pexpiretime(3valkey),pttl(3valkey),randomkey(3valkey),rename(3valkey),renamenx(3valkey),restore(3valkey),scan(3valkey),sort(3valkey),sort_ro(3valkey),touch(3valkey),ttl(3valkey),type(3valkey),unlink(3valkey),wait(3valkey),waitaof(3valkey)
okay that's the whole page bestie, go forth and pattern-match responsibly π
version 9.0.0, dated 2025-10-21 β that's the KEYS(3valkey) manual page, mwah π
π¦ 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