HGETALL for all fields in Redis - Time & Space Complexity
When we ask how long a Redis command takes, we want to know how its work grows as the data grows.
For HGETALL, we want to see how the time changes when the number of fields in a hash changes.
Analyze the time complexity of the following Redis command.
HGETALL myhash
This command fetches all fields and their values from the hash named 'myhash'.
Look for repeated work inside the command.
- Primary operation: Reading each field and its value in the hash.
- How many times: Once for every field stored in the hash.
As the number of fields grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads |
| 100 | About 100 reads |
| 1000 | About 1000 reads |
Pattern observation: The work grows directly with the number of fields. Double the fields, double the work.
Time Complexity: O(n)
This means the time to run HGETALL grows in a straight line with the number of fields in the hash.
[X] Wrong: "HGETALL is always fast no matter how big the hash is."
[OK] Correct: Because HGETALL reads every field, if the hash is very large, it takes longer. It's not instant for big hashes.
Understanding how commands like HGETALL scale helps you explain performance clearly and shows you know how data size affects speed.
"What if we used HGET to get only one field instead of HGETALL? How would the time complexity change?"