QuickRandomRecords

quick_random_records is a Ruby Gem that empowers ActiveRecord Model to return random records dramatically fast, even with tables that have lots 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 common random records strategies

Scenario: query 100 random records from table with 550,000 data rows in localhost.

  1. quick_random_records costs only 6.2ms totally. alt text
  2. Model.order("RAND()").limit(num) costs 3314.1ms. alt text
  3. Model.where(id: Model.pluck(:id).sample(num)) costs 1659.4ms totally. alt text

alt text

alt text

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 selects records 1.05 times more than required. So that it doesn't need to perform another query to make complements. And of course, it will truncate to required number of records before method return.

You can configure your own multiply, which is 1.05 by default. EX: If table has 10% deleted records, multiply 1.1 will maximize the speed of random_records.

 # select 1.1 times more than required, that is 110 in this case. 
 # 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 records 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 supported by this GEM

users = User.random_records(100, strategy: 2)

Model.where(id: Model.pluck(:id).sample(num)) is strategy 3 supported by this GEM

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.