mercredi 24 février 2016

Select nth rails record from huge set

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?

Aucun commentaire:

Enregistrer un commentaire