我有950行,我正在通過滑軌使用下面的控制器代碼導入到Postgres的一個CSV:對Postgres進行Rails CSV導入需要多少時間?
io = params[:file]
trade_plans = []
CSV.foreach(io.tempfile, { :col_sep => "|" }) do |row|
if row.length == 16
trade_plans << create_trade_plan(row)
end
end
TradePlan.create(trade_plans)
def create_trade_plan(row)
number_of_shares = row[10].to_i.abs
return {
:user_id => current_user.id,
:symbol => row[2].upcase,
:symbol_long_name => row[3].upcase,
:direction => STYPE_MAP[row[5]][1],
:number_of_shares => number_of_shares,
:description => "Trade #{row[1]} on #{format_datetime(row[7], row[8])}",
:trade_status => 'closed',
:is_potential => false,
:planned_entry_price => 0,
:planned_target_price => 0,
:planned_stop_loss_price => 0,
:without_trade_plan => true
}
end
我使用ActiveRecord的散裝create
方法,雖然從this answer它似乎是批量插入ISN 「T支持Postgres的......反正,想我所知道的是,我的Rails日誌給我這個:
Completed 200 OK in 315855ms (Views: 1280.1ms | ActiveRecord: 3341.2ms)
如果只有3.3秒AR,並導入如何利用315秒爲950行?有幾個回調和一些驗證,但對我來說這似乎很重要。每記錄0.33秒是否合理?
每個插入操作是在其自己的事務中完成還是分批進行?桌子上是否有外鍵?對底層insert的一個'explain analyze'進行解釋(看PostgreSQL日誌,可能使用'log_statement ='all''啓用)。你可以使用Pg Gem對PostgreSQL'COPY'的支持來做一個快速的流媒體導入嗎? –