Skip to content

Instantly share code, notes, and snippets.

@pbyrne
Created September 27, 2012 16:28
Show Gist options
  • Save pbyrne/3794956 to your computer and use it in GitHub Desktop.
Save pbyrne/3794956 to your computer and use it in GitHub Desktop.
Performance Comparison
# plain ActiveRecord
Benchmark.measure do
User.where(:activated => true).limit(200000).map(&:first_name)
end
# => 58.210000 12.970000 71.180000 ( 73.134415)
# writing the improved SQL by hand
Benchmark.measure do
ActiveRecord::Base.connection.select_all("select * from users where activated = 1 limit 200000").map { |attrs| attrs[:first_name] }
end
# => 45.910000 12.850000 58.760000 ( 60.632703)
# better performance but ugly as sin
# Using Arel and find_as_hashes
Benchmark.measure do
User.where(:activated => true).limit(200000).all_as_hashes.map { |attrs| attrs[:first_name] }
end
# => 45.950000 13.040000 58.990000 ( 60.826395)
# ah, the improved performance with the benefit of Arel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment