title: Does a redis scan with a prefix match pattern scan all keys in database?
body: <p>Let's say I have a redis instance with a million keys. The keys contain a team ID and a person ID and the values have some info about that team-person association.  Example keys:</p>
  
  <pre><code>team:1:person:123
  team:2:person:234
  team:2:person:345
  ...
  </code></pre>
  
  <p>I could use a <code>scan</code> with a pattern to get all the people on a given team. For example, <code>scan 0 match "team:123:person:*"</code>, would start getting everyone on team 123.</p>
  
  <p>It sounds like this is <em>not</em> an efficient way to iterate over people in a team, because scan is O(N) where N is the number of keys in the <em>database</em>. But I want to confirm - is that the case? </p>
  
  <p>A database with a tree index might only need to scan a small section of the tree, with keys with that prefix. </p>
  
  <p>If it does scan everything, then I guess if I want to iterate quickly over people in a team, I need to store the set of person IDs under a team ID. Something like:</p>
  
  <pre><code>sset team:2:people 234 345 ...
  </code></pre>