批量插入记录到活动记录表批量

2023-09-08 15:45:59 作者:悲伤的季节

我发现我的 Model.create!语句服用很长一段时间,当我加入了大量的记录一次运行。看着 ActiveRecord的导入但它并没有使用哈希数组的工作(这是我已经和这我认为这是常见的pretty的)。如何提高性能?​​

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?

推荐答案

我开始运行到出现问题大量记录(> 10000),所以我修改了code在1000组记录在工作时间。这里是一个链接到新的code:

I started running into problems with large numbers of records (> 10000), so I modified the code to work in groups of 1000 records at a time. Here is a link to the new code:

https://gist.github.com/jackrg/76ade1724bd816292e4e