1

We have tens of millions of records in our User model. Our CEO wants to know who every single n-millionth user is. I'm trying to find a performant way to do this.

If I were trying to do find the 10 millionth record, I've tried to do

User.select(:id).where(deleted_at:nil)[9999999]

and

User.find(User.last.id - User.where(deleted_at:nil).count + 9999999)

I've tried to monkey patch the find_nth method from finder methods, but that hasn't worked in a performant way as well.

I get that "performant" may be a stretch, but I'm interested in the most efficient way to do this. Any suggestions?

renegadeborealis
  • 284
  • 1
  • 3
  • 9

1 Answers1

2

You can do something like this:

User.order(:id).where(deleted_at:nil).offset(10_000_000).limit(1)

That will get you the 10 millionth user. You can then loop over this, increasing the offset each time, until you stop getting a user.

You can use any order you want, e.g. created_at, but if it's not an indexed column, it'll probably be pretty slow.

James Bowden
  • 191
  • 4