Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk Insert records into Active Record table

I found that my Model.create! statements were taking a very long time to run when I added a large number of records at once. Looked at ActiveRecord-Import but it didn't work with an array of hashes (which is what I have and which I think is pretty common). How can I improve the performance?

like image 700
Jack R-G Avatar asked Mar 10 '13 01:03

Jack R-G


1 Answers

Use the activerecord-import gem. Let us say you are reading a CSV file and generating a Product catalogue and you want to insert records in batches of 1000:

batch,batch_size = [], 1_000 
CSV.foreach("/data/new_products.csv", :headers => true) do |row|
  batch << Product.new(row)

  if batch.size >= batch_size
    Product.import batch
    batch = []
  end
end
Product.import batch
like image 158
Harish Shetty Avatar answered Sep 28 '22 06:09

Harish Shetty