Last active
May 11, 2018 10:59
-
-
Save panmari/73a2c203d24e7e9461d1 to your computer and use it in GitHub Desktop.
Benchmark of various methods to retrieve a random record from a table using ActiveRecord and a Postgresql db behind.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
# Disable logging if in development/test mode | |
ActiveRecord::Base.logger = nil | |
model_name = Icd | |
N = 100 | |
Benchmark.bm(11) do |x| | |
x.report('ruby rand') { N.times { model_name.offset(rand(model_name.count)).first } } | |
x.report('psql RANDOM') { N.times { model_name.order('RANDOM()').first } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a fairly large table with 131883 rows, I got these timings:
using Rails 4.2, Postgresql 9.4