title: Inserting data from CSV into MySQL DB is very slow
body: <p>Trying to insert data from a CSV file to a MySQL DB using Ruby, and it's very slow. Note that this is not a Rails application, just stand-alone Ruby script.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>def add_record (data1, data2, time)
      date = DateTime.strptime(time, "%m/%d/%y %H:%M")
      &lt;my table&gt;.create(data1: data1, data2: data2, time: date)
  end
  
  def parse_file (file)
      path = @folder + "\\" + file
      CSV.foreach(path, {headers: :first_row}) do |line|
          add_record(line[4], line[5], line[0])
      end
  end
  
  def analyze_data ()
      Dir.foreach @folder do |file|
          next if file == '.' or file == '..'
          parse_file file
      end     
  end
  </code></pre>
  
  <p>And my connection:</p>
  
  <pre><code>@connection = ActiveRecord::Base.establish_connection(  
    :adapter=&gt; "mysql2",  
    :host =&gt; "localhost",  
    :database=&gt; &lt;db&gt;,
    :username =&gt; "root",
    :password =&gt; &lt;pw&gt;
  )
  </code></pre>
  
  <p>Any help appreciated.</p>