0

I need to pick a random record for a VERY large ActiveRecord set.

What's the best way to do this? I have something like this, but it still takes a long time.

Model.select('id').where("id = ? AND attr = ?", self.id, false).offset(rand(size)).limit(1)

Thank you

neofetter
  • 3,004
  • 2
  • 26
  • 26

2 Answers2

0

The usual techniques for this rely on generating a series of random numbers that represent the possible id's of records, and pulling back the relevant row.

You generally need to read the maximum (and maybe minimum) id's, and generate a bunch of random numbers between them. You generally can't guarantee that every id will exist, hence generating quite a few (or looping with a new random number each time) until you find one for which a row exists.

David Aldridge
  • 51,479
  • 8
  • 68
  • 96
-1
Model.select('id').where("id = ? AND attr = ?", self.id, false).order("RANDOM()").first

Seems OK, but I feel like there is a better way.

neofetter
  • 3,004
  • 2
  • 26
  • 26