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

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

问题描述

我发现当我一次添加大量记录时,我的 Model.create! 语句需要很长时间才能运行.查看 ActiveRecord-Import 但它不适用于哈希数组(这是我所拥有的我认为很常见).如何提高性能?

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) 的问题,因此我修改了代码以一次处理 1000 条记录.这是新代码的链接:

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

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

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