我有一個CSV文件,我用它來更新數據。更新不超過15000條記錄需要很長時間(大約10分鐘)。 這是我使用的代碼:緩慢導入CSV到數據庫中
task csv_updater_so: :environment do
require 'csv'
counter = 0
time = Benchmark.realtime do
save_folder = Rails.root.join('path_to_file')
CSV.foreach(save_folder, encoding:'iso-8859-1:utf-8', col_sep: ';', :quote_char => "\x00", headers: true) do |row|
#If the item is in the db I update its values
if item = Item.find_by_internal_code(row[4])
item.update(:price => row[9], :stock_b => row[10])
end
counter += 1
print "updated items => #{counter}" + "\r"
end
end
end
我覺得很奇怪,以更新從它只需一堆秒的xml文件相同的模型。難道我做錯了什麼?
我也曾嘗試
task csv_updater_so: :environment do
require 'csv'
counter = 0
time = Benchmark.realtime do
save_folder = Rails.root.join('path_to_file')
updateable_items = CSV.foreach(save_folder, encoding:'iso-8859-1:utf-8', col_sep: ';', :quote_char => "\x00", headers: true).map do |row|
if item = Item.find_by_internal_code(row[4])
item.update(:price => row[9], :stock_b => row[10])
end
counter += 1
print "updated items => #{counter}" + "\r"
end
Item.import(updateable_items)
end
end
您使用的是什麼版本的Ruby和Rails?舊的CSV庫確實非常慢。 (我問的是因爲'Item.find_by_internal_code'是一種老式的查找方法。) – Raffael
Ruby 2.3.3和Rails 4.2.6 耗時的部分似乎是更新方法,但即使我使用「touch」沒有改進。 – Jack
因此,如果刪除更新行,腳本會在幾秒鐘內再次運行? – Raffael