Skip to content

Instantly share code, notes, and snippets.

@sahidursuman
Forked from derwiki/bulk_insert.rb
Created May 31, 2018 08:57
Show Gist options
  • Save sahidursuman/46fd06aca69056f01c1a294779a302d2 to your computer and use it in GitHub Desktop.
Save sahidursuman/46fd06aca69056f01c1a294779a302d2 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