简体   繁体   中英

In rails how to Insert thousands of records in table

I have created API using Ruby on Rails. when I call an API endpoint to insert more than 1000 records it gets crashed. currently, I am using database Mysql2

my code is

quantity.times do
  User.create(first_name: "John")
end

where quantity is a number of records need to insert and it can be any number like 10000, 4000000.

can anyone suggest me how can I do this very efficiently without breaking my server

ActiveRecord.import

items = []
quantity.times.each do |row|
  items << User.new(name: "john")
end
User.import(items)

Just have a look at these benchmarks to get a sense of just how much faster this option is. Instead of separate transactions, commits, and inserts that a Model.create generates SQL for, this handles it in a single query.

References

I suggest you can use find_each and specify the batch_size to loop it.

I think it will prevent the crash in case you loop a huge amount of quantity same as your example

try this gem 'activerecord-insert_many' . In the source code, find tests and you can see how to consume the gem.

Thanks, Ajith

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM