简体   繁体   English

将巨大的Excel文件导入Rails应用程序

[英]Importing huge excel file to Rails application

I have an excel file with thousands of rows. 我有一个具有数千行的Excel文件。 In my case, I can't use bulk insert, because for each row I should create few associations. 就我而言,我不能使用批量插入,因为我应该为每一行创建几个关联。 Now, all process take more than 1 hour with 20k rows, which is hell. 现在,所有过程都需要1个多小时,包含2万行,这真是太糟糕了。 What is the best way to resolve this problem? 解决此问题的最佳方法是什么?

I'm using spreadsheet gem . 我正在使用电子表格gem

This is analogous to the infamous "1+N" query situation that Rails loves to encounter. 这类似于Rails喜欢遇到的臭名昭著的“ 1 + N”查询情况。 I have a similar situation (importing files of 20k+ rows with multiple associations). 我有一个类似的情况(导入具有多个关联的20k +行的文件)。 The way I optimized this process was to pre-load hashes for the associations. 我优化此过程的方法是为关联预加载哈希。 So for example, if you have an AssociatedModel that contains a lookup_column that is in your import data, you would first build a hash: 因此,例如,如果您有一个AssociatedModel包含导入数据中的lookup_column ,则首先要构建一个哈希:

associated_model_hash = Hash.new(:not_found)

AssociatedModel.each do |item|
  associated_model_hash[item.lookup_column] = item
end

This provides a hash of objects. 这提供了对象的哈希。 You can repeat for as many associations as you have. 您可以重复创建尽可能多的关联。 In your import loop: 在您的导入循环中:

associated_model = associated_model_hash[row[:lookup_column]]
new_item.associated_model_id = associated_model.id

Because you don't have to do a search on the database each time, this is much faster. 因为您不必每次都在数据库上进行搜索,所以这要快得多。 It should also allow you to use bulk insert (assuming you can guarantee that the associated models will not be deleted or modified in a bad way during the load). 它还应该允许您使用批量插入(假设您可以保证在加载期间不会以不良方式删除或修改关联的模型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM