批量插入记录到活动记录表 [英] Bulk Insert records into Active Record table

查看:172
本文介绍了批量插入记录到活动记录表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的 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

这篇关于批量插入记录到活动记录表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆