Skip to content

Instantly share code, notes, and snippets.

@derwiki
Created November 3, 2014 19:23
Show Gist options
  • Save derwiki/a447d1290f6fd4ac1310 to your computer and use it in GitHub Desktop.
Save derwiki/a447d1290f6fd4ac1310 to your computer and use it in GitHub Desktop.
Comparing runtime characteristics of individual transactions, one transaction, and one statement.
def insert_many
1000.times { SeoRank.create! }
end
start = Time.now
ActiveRecord::Base.transaction { insert_many }
puts "N transactions: #{ Time.now - start }"
start = Time.now
ActiveRecord::Base.transaction { insert_many }
puts "One transaction: #{ Time.now - start }"
values = (["('a')"] * 1000).join(',')
start = Time.now
ActiveRecord::Base.connection.execute "insert into seo_ranks (href) values #{ values }"
puts "Hand rolled SQL: #{ Time.now - start }"
$ be rails runner "$( cat transaction.rb )"
N transactions: 1.390017
One transaction: 1.110242
Hand rolled SQL: 0.011879
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment