Skip to content

Instantly share code, notes, and snippets.

@ilake
Last active December 11, 2015 01:59
Show Gist options
  • Select an option

  • Save ilake/4527383 to your computer and use it in GitHub Desktop.

Select an option

Save ilake/4527383 to your computer and use it in GitHub Desktop.
def self.query_benchmark
keys = Visit.column_names
total = Visit.count
Benchmark.bm(7) do |x|
# 346.010000 1.380000 347.390000 (357.346086)
# 361.920000 1.650000 363.570000 (417.746442)
x.report("ActiveRecord") do
Visit.find_in_batches(:batch_size => 5000) do |visits|
visits.map{|v| keys.map{|k| v.send(k.to_sym)}}
end
end
# 1613.550000 6.760000 1620.310000 (1667.230748)
x.report("ActiveRecord json") do
Visit.find_in_batches(:batch_size => 5000) do |visits|
visits.to_json
end
end
# 58.640000 0.780000 59.420000 ( 67.759693)
# 64.520000 0.920000 65.440000 (142.426560)
x.report("Pure") do
primary_key_offset = 0
begin
sql = "SELECT * FROM `visits` WHERE (id > #{primary_key_offset}) ORDER BY `id` ASC LIMIT 5000"
rows = ActiveRecord::Base.connection.execute(sql)
primary_key_offset = rows.to_a.last[rows.fields.index("id")] if rows.any?
end while rows.any?
end
# 74.600000 1.380000 75.980000 ( 85.274206)
fields = ReportingSerializerConfig[:all].reverse_merge(ReportingSerializerConfig[:schemas]['Visit']).to_a.map{|field| field.join(' as ')}.join(', ')
x.report("Pure") do
primary_key_offset = 0
begin
sql = "SELECT #{fields} FROM `visits` WHERE (id > #{primary_key_offset}) ORDER BY `id` ASC LIMIT 5000"
rows = ActiveRecord::Base.connection.execute(sql)
data_array = rows.to_a(:as => :hash)
primary_key_offset = data_array.last["mysql_id"] if data_array.any?
end while data_array.any?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment