Does ActiveRecord perform inserts/deletes in bulk when inside a transaction? Does ActiveRecord perform inserts/deletes in bulk when inside a transaction? ruby-on-rails ruby-on-rails

Does ActiveRecord perform inserts/deletes in bulk when inside a transaction?


For inserts:

ActiveRecord does not perform a bulk insert when using a transaction. However it does speed things up a bit since it is using a single transaction to execute all INSERT statements as opposed to one transaction per INSERT statement otherwise.

So:

Queue.transaction do   @queue.each do |row|    # an INSERT is done here  endend

is going to be faster than:

@queue.each do |row|  # an INSERT is done hereend

For more info on how to really do bulk inserts, check out this article.

For deletes:

The ActiveRecord delete_all call is one single SQL DELETE statement, so I guess you could consider this as a bulk delete (no need to use a transaction here since it's already encapsulated in one transaction by ActiveRecord). This is not the case when calling delete on each record, which will result in multiple SQL DELETE statements, thus multiple transactions initiated and committed and overall slower performance.


I suggest you take a look at ActiveRecord Import: https://github.com/zdennis/activerecord-import.

I'm using this tool to insert between 10,000 and 100,000 rows of data.

books = []10.times do |i|   books << Book.new(:name => "book #{i}")endBook.import books

If you're using MySQL, it also supports ON DUPLICATE KEY UPDATE so you can intelligently insert new / update old rows. https://github.com/zdennis/activerecord-import/wiki/MySQL:-On-Duplicate-Key-Update-Support