How to perform bulk insert into database when model is a nested set?

231 views Asked by At

I have a model involving nested set data structure, so I have used act_as_nested_set gem. Now, I have a requirement of bulk inserting more 100 thousand records. If it was not nested set data structure, bulk insertion could have been easier and swift. But with Nested set each insertion update all the records in the ancestor chain. So, this takes too long.

I thought of creating in-memory tree and maintaining the lft, rgt, depth, children_count, depth and then mass inserting them, but I am not certain if it would be good idea to create 100 thousands complex object in-memory.

1

There are 1 answers

1
Rohit Lingayat On

you can use activerecord-import gem, Activerecord-Import is a library for bulk inserting data using ActiveRecord.

GitHub link: https://github.com/zdennis/activerecord-import

e.g.

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