简体   繁体   中英

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

In a Rails application, I'm using this to get a random row from a MySQL table:

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

Even though the table only has about 20,000 rows it takes a couple seconds. Does anyone know of a faster way?


Update

Also tried just running the SQL in the console...

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

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

从红宝石获得一兰特,然后将其转换为您的主键

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM