0

これは私のインポートスクリプトです。私は約40万件のレコードを持っています。そして、レコードの挿入中に接続が閉じます。それをより小さな断片に分割する方法は?

  def extract_to_database(collection)
    tmp       = []
    type      = K
    inserts = []

    collection.each_with_index do |line, i|
      _type    = line.strip
      _changed = TYPES.include? _type

      if _changed && i > 0
        case type
        when K then @k << tmp
        when F then @f << tmp
        when FB then @f << tmp
        when I, D
          pharmaceutic = Pharmaceutic.find_by pzn: tmp[PZN]
          if pharmaceutic
            inserts.push "(#{pharmaceutic.id}, '#{tmp[UNIT]}', '#{tmp[DOSE]}')"
          end
        end

        tmp  = []
        type = _type
      end

      tmp << clean(line)
    end
    sql = "INSERT INTO pharmaceutic_dosages (`pharmaceutic_id`, `unit`, `dose`) VALUES #{inserts.join(", ")}"
    CONN.execute sql
  end
4

3 に答える 3