Last active
August 29, 2015 14:20
-
-
Save Guitaronin/efbeb9974dedda7d6486 to your computer and use it in GitHub Desktop.
benchmark activerecord loading associations
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' | |
puts "loading..." | |
APP_PATH = File.expand_path('../../config/application', __FILE__) | |
require File.expand_path('../../config/boot', __FILE__) | |
require File.expand_path('../../config/environment', __FILE__) | |
puts "running..." | |
selections = ['source_id', 'campaign', 'sum(cost) as cost'] | |
conditions = {source_name: 'google'} | |
groupings = ['source_id', 'campaign'] | |
included = ['source'] | |
n = 10 | |
Benchmark.bm(17) do |x| | |
x.report("no preload:") do | |
n.times do | |
MarketingCost.select(selections).where(conditions).group(groupings).to_a.map { |c| c.source } | |
end | |
end | |
x.report("with cache:") do | |
n.times do | |
ActiveRecord::Base.connection.cache do | |
costs = MarketingCost.select(selections).where(conditions).group(groupings).to_a.map { |c| c.source } | |
end | |
end | |
end | |
x.report("includes:") do | |
n.times do | |
MarketingCost.select(selections).where(conditions).group(groupings).includes(included).to_a.map { |c| c.source } | |
end | |
end | |
x.report("includes w/cache:") do | |
n.times do | |
ActiveRecord::Base.connection.cache do | |
MarketingCost.select(selections).where(conditions).group(groupings).includes(included).to_a.map { |c| c.source } | |
end | |
end | |
end | |
x.report("preload:") do | |
n.times do | |
MarketingCost.select(selections).where(conditions).group(groupings).preload(included).to_a.map { |c| c.source } | |
end | |
end | |
x.report("eager_load:") do | |
n.times do | |
MarketingCost.select(selections).where(conditions).group(groupings).eager_load(included).to_a.map { |c| c.source } | |
end | |
end | |
x.report("joins:") do | |
n.times do | |
MarketingCost.select(selections).where(conditions).group(groupings).joins(*included.map { |i| i.to_sym }).to_a.map { |c| c.source } | |
end | |
end | |
x.report("joins w/cache:") do | |
n.times do | |
ActiveRecord::Base.connection.cache do | |
MarketingCost.select(selections).where(conditions).group(groupings).joins(*included.map { |i| i.to_sym }).to_a.map { |c| c.source } | |
end | |
end | |
end | |
end | |
# user system total real | |
# no preload: 8.340000 0.350000 8.690000 ( 9.664360) | |
# with cache: 6.420000 0.110000 6.530000 ( 6.630513) | |
# includes: 14.690000 0.050000 14.740000 ( 14.912824) | |
# includes w/cache: 14.110000 0.030000 14.140000 ( 14.193161) | |
# preload: 14.050000 0.030000 14.080000 ( 14.134771) | |
# eager_load: 2.650000 0.030000 2.680000 ( 2.693203) | |
# joins: 8.520000 0.350000 8.870000 ( 10.112449) | |
# joins w/cache: 6.280000 0.100000 6.380000 ( 6.414173) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment