在Rails中批量插入时出错

时间:2017-06-29 14:32:58

标签: ruby-on-rails ruby postgresql activerecord bulkinsert

我正在尝试从不同的MySQL数据库在Rails ActiveRecord中进行批量插入。我的数据库是Postgres。

使用以下代码和gem bulk_insert

batch,batch_size = [], 1_000
records.each do |row|
    batch << params
    if batch.size >= batch_size
        TableName.bulk_insert values: batch
        batch = []
    end
end

但是我在尝试这样做时遇到了错误。前1000个记录插入正常。之后我得到以下内容:

from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:78:in `block in save!'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:78:in `tap'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:78:in `save!'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:40:in `add'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:63:in `block in add_all'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:63:in `each'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert/worker.rb:63:in `add_all'
from /var/lib/gems/2.3.0/gems/bulk_insert-1.5.0/lib/bulk_insert.rb:13:in `block in bulk_insert'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
... 22 levels...
from /var/lib/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/console_helper.rb:9:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:78:in `console'
from /var/lib/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /var/lib/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `require'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `block in require'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:259:in `load_dependency'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:293:in `require'
from /home/user/rails-app/bin/rails:9:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `load'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `block in load'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:259:in `load_dependency'
from /var/lib/gems/2.3.0/gems/activesupport-5.0.3/lib/active_support/dependencies.rb:287:in `load'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'

记录的大小约为100,000。什么是在Rails表中插入这些数量的记录的最佳方法?

我做错了吗?

1 个答案:

答案 0 :(得分:0)

我猜你可以使用ActiveRecord导入库 请阅读this

bulk_insert