Created
September 27, 2012 16:28
-
-
Save pbyrne/3794956 to your computer and use it in GitHub Desktop.
Performance Comparison
This file contains hidden or 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
# 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