Search code examples
mysqlruby-on-railsrubylimitoffset

Why is record missing while using limit and offset in mysql?


Lets say I have a table Product. I have few thousands record in my table.

When I tried The offset_val = 0,10,20,30...

Product.where("live" => true).offset(offset_val).limit(10).order("position DESC")

Executing the same query with different offset_val,

It returns me the live product only 10 at a time, but found some records are missing in the result

Any constraint or situation will have this problem?

  Sample Record Set =[0,1,2,3,4,5,6,7,.....,20]
    Expected Output 
    when offset_val = 10  & limit = 10
    => [10,11,12,13,14,15,16,17,18,19]
   Actual Output => [10,11,12,13,15,16,17,18,20,21]

Solution

  • just make sure that "live" => true for the missing records