【发布时间】:2017-08-17 09:06:49
【问题描述】:
我目前正在使用 Ruby/Rails 进行一个项目,将发票导入数据库,但试图最大限度地提高流程的效率,这确实太慢了。
对于具有 100.000 行的导入批次,处理和保存数据库中的每条记录大约需要 2.5 3 小时。
//// Ruby 代码
class DeleteImportStrategy
def pre_process(merchant_prefix, channel_import)
# channel needed to identify invoices so an import from another channel cannot collude if they had same merchant_prefix
Jzbackend::Invoice.where(merchant_prefix: merchant_prefix, channel: channel_import.channel).delete_all
# get rid of all previous import patches which becomes empty after delete_import_strategy
Jzbackend::Import.where.not(id: channel_import.id).where(channel: channel_import.channel).destroy_all
end
def process_row(row, channel_import)
debt_claim = Jzbackend::Invoice.new
debt_claim.import = channel_import
debt_claim.status = 'pending'
debt_claim.channel = channel_import.channel
debt_claim.merchant_prefix = row[0]
debt_claim.debt_claim_number = row[1]
debt_claim.amount = Monetize.parse(row[2])
debt_claim.print_date = row[3]
debt_claim.first_name = row.try(:[], 4)
debt_claim.last_name = row.try(:[], 5)
debt_claim.address = row.try(:[], 6)
debt_claim.postal_code = row.try(:[], 7)
debt_claim.city = row.try(:[], 8)
debt_claim.save
end
结束
////
因此,对于以 CSV 形式出现的每个导入批次,我会删除以前的批次,并通过读取每一行并将其插入到新的 Import as Invoice 记录中来开始导入新批次。但是,100.000 个条目需要 2.5-3 小时似乎有点过大。我如何优化这个过程,因为我确信这种方式肯定没有效率。
编辑: 因此,我已经很久没有发布此内容了,但请注意,我最终使用了 activerecord-import 库,该库从那时起运行良好。但是请注意,它的 :on_duplicate_key_update 功能仅在 PostgreSQL v9.5+ 中可用。
【问题讨论】:
标签: ruby-on-rails ruby postgresql activerecord benchmarking