ruby - buld_insert 这个gem很好用啊

访问量: 902

参考 https://github.com/jamis/bulk_insert

原来的一条一条的插入,大约需要5分钟:

  data.each do |row|
    (1990..1999).each_with_index do |year, index|
      OneDegreeCell.create longitude: row[0].to_i, latitude: row[1].to_i, year: year, value: row[2 + index]
    end 
  end 

改成批量插入后,11ms 搞定:

OneDegreeCell.bulk_insert do |worker|
  data.each do |row|
    (1990..1999).each_with_index do |year, index|
      worker.add longitude: row[0].to_i, latitude: row[1].to_i, year: year, value: row[2 + index]
    end 
  end 
end

订阅/RSS Feed

Subscribe