title: Powershell and MySQL parameterized query performance
body: <p>I've run into a performance issue with Powershell and generating paramterized queries. I'm pretty sure that I'm going about it wrong and hopefully someone can help.</p>
  
  <p>Here is the problem: inserting 1000 rows (with about 15 columns each) takes around 3 minutes when using parameterized queries. Using non-parameterized batch inserts takes less than a second. The problem seems to lie in how I'm generating the parameters as that is the part of the code that eats most of the time. The looping is killing the performance. The problem wouldn't be a big deal if all I had is 1000 rows to deal with  -however, there are millions.</p>
  
  <p>Here is what I have so far. I've omitted quite a bit for the sake of brevity. But, don't get me wrong  -it certainly works. Its just painfully slow.</p>
  
  <pre><code>$values = @() #some big array of values
  $sqlCommand.CommandText = "INSERT INTO ``my_db``.``$table`` (dataseries_code,dataseries_text) VALUES (?,?),(?,?)" #abbreviated for for the sake of sanity
  
  for($i = 0; $i -lt $values.Count; $i++) {
      $sqlCommand.Parameters.AddWithValue($i, $values[$i]) #this is the slow part
  }
  
  $insertedRows = $sqlCommand.ExecuteNonQuery()
  Write-Host "$insertedRows"
  $sqlCommand.Parameters.Clear()
  </code></pre>
  
  
  
  <p>There must be a better way to generate the .Parameters.AddWithValue statement? I took a look at the .AddRange method but couldn't figure how to make it work and if it was even intended for what I'm try to do.</p>
  
  <p>EDIT:
  I should have mentioned that I also tried creating the parameters first, and then adding the values. It took 3 minutes to both create the parameters, and 3 minutes to add the values! (see example below). 6 minutes total for 30,000 rows! Something is not right.</p>
  
  <p>I can't help but think that there has to be a faster method to do parameterized inserts?!</p>
  
  <pre class="lang-cs prettyprint-override"><code>#THIS PIECE WILL ONLY RUN ONCE
  #this is where we create the parameters first
  for($i = 0; $i -lt @($values).Count; $i++) {
      $command.Parameters.Add((New-Object MySql.Data.MySqlClient.MySqlParameter($i[MySql.Data.MySqlClient.MySqlParameter]::VarChar))) | out-null
  }
  
  #THIS PIECE WILL RUN MULTIPLE TIME
  #and this is where we add the values
  for($i = 0; $i -lt @($values).Count; $i++) {
      $command.Parameters[$i].Value = $values[$i] | Out-Null
  }
  </code></pre>
  
  <p>Thanks!</p>