QuickRandomRecords
quick_random_records is a Ruby Gem that empowers ActiveRecord Models with the ability to return random records dramatically fast, even with table that has a lot of data rows.
Installation
Add this line to your application's Gemfile:
gem 'quick_random_records'
And then execute:
$ bundle
Or install it yourself as:
$ gem install quick_random_records
Usage
# return ActiveRecord::Relation contains 10 random model objects from User Table
users = User.random_records(10)
Dramatically fast, compared to other random records strategies
Scenario: query 100 random records from table with 550,000 data rows.
quick_random_recordscosts25.0ms.
Model.order("RAND()").limit(num)costs3314.1ms.
Model.where(id: Model.pluck(:id).sample(num))costs1659.4ms.


Fine-tuning
This strategy is fast because:
(1) Instead of plucking all id in the table, it selects id bewteen min_id and max_id. Then make complements if any missing records (id between min_id and max_id, but not exist in db).
(2) It select id 1.25 times more than required. So that it doesn't need to perform another query to make complements. And of course, it will truncate to the required number before method return.
You can configure your own multiply, which is 1.25 by default. EX: My table has 10% deleted records, so multiply 1.1 will maximum the speed of random_records.
# select 1.1 times more than required, that is 110 here.
# And it will truncate to 100 before method return.
users = User.random_records(100, multiply: 1.1)
Drawback
This strategy works extremely well with table that has a lot of records and few deleted records.
But for tables with a lot of deleted records (ex: 8 deleted reocrds out of 10 records), it may return fewer random records as required since I limit the loop searching for complements.
The default loop_limit is 3. You can configure your own loop_limit for searching complements.
users = User.random_records(100, loop_limit: 5)
or
You can use other strategy for tables with a lot of deleted records.
Model.order("RAND()").limit(num) is strategy 2
users = User.random_records(100, strategy: 2)
Model.where(id: Model.pluck(:id).sample(num)) is strategy 3
users = User.random_records(100, strategy: 3)
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/quick_random_records. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.