0

I am using Ruby on Rails to store keys into redis. There are more than 20000 keys under my hash. I tried to limit 10 while getting all. But the redis returning 11 instead of 10. Why its returning 11?

My hash,

127.0.0.1:6379> HLEN myhash
(integer) 14148

In the following example, am querying 4 records but returning 5 with corresponding values,

127.0.0.1:6379> HSCAN my_hash 0 match * count 4
1) "896"
2)  1) "player 1"
    2) "{\"id\":\"13192\",\"name\":\"Ram\"}"
    3) "player 2"
    4) "{\"id\":\"5429\",\"name\":\"Gopal\"}"
    5) "player 3"
    6) "{\"id\":\"2614\",\"name\":\"Venu\"}"
    7) "player 4"
    8) "{\"id\":\"9645\",\"name\":\"Sam\"}"
    9) "player 5"
   10) "{\"id\":\"2358\",\"name\":\"Rohit\"}"

Why am getting 5 here?

SST
  • 2,054
  • 5
  • 35
  • 65

2 Answers2

2

The documented meaning of COUNT is that it's a hint as to how much work to do on the HSCAN call, it's not a strict number of elements to return.

While SCAN does not provide guarantees about the number of elements returned at every iteration, it is possible to empirically adjust the behavior of SCAN using the COUNT option. Basically with COUNT the user specified the amount of work that should be done at every call in order to retrieve elements from the collection. This is just a hint for the implementation....

When iterating the key space, or a Set, Hash or Sorted Set that is big enough to be represented by a hash table, assuming no MATCH option is used, the server will usually return count or a bit more than count elements per call.

Community
  • 1
  • 1
Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
2

check this answer

According to the docu:

"When iterating the key space, or a Set, Hash or Sorted Set that is big enough to be represented by a hash table, assuming no MATCH option is used, the server will usually return count or a bit more than count elements per call. Please check the why SCAN may return all the elements at once section later in this document. "

Gilg Him
  • 842
  • 10
  • 19