KEYS(3valkey)

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

✨ 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: consider KEYS a 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 use KEYS in your regular application code. if you're looking for a way to find keys in a subset of your keyspace, consider using valkey-scan(7) SCAN or sets instead. periodt πŸ’€

supported glob-style patterns, let's go:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, ... but not hello
  • h[a-b]llo matches hallo and hbllo

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 🌈


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