Is there a faster way to get a random record from a MySQL table via ActiveRecord?

In a Rails application, I use this to get a random row from a MySQL table:

contact = Contact.find(:all, :limit => 1, :order => 'RAND()')[0]

      

Despite the fact that the table contains about 20,000 rows, it takes a couple of seconds. Does anyone know a faster way?


Update

Also tried to just run SQL in the console ...

SELECT * FROM `contacts` ORDER BY RAND() LIMIT 1

      

It still takes a long time, like two or three seconds.

0


a source to share


2 answers


I don't think my first answer will help you. Perhaps this article will



+2


a source


Get one rand from ruby ​​and then convert it to your primary key



0


a source







All Articles