title: foreach INSERT into Mysql DB is really slow, specially when inserting over 100K records and over
body: <p>How could I make this code faster. It's fine when I insert 100 records into my database but it takes really long time when I insert let's say 500K records.
  I've tried to use implode in my code but it's not working.
  Code seems to have two foreach loops, one inside the other, but I can't find a way to make it work, does anyone has an idea? 
  My framework is codeigniter.</p>
  
  <p>Here's what the code looks like:</p>
  
  <pre><code>&lt;?php  
   function Add_multiple_users($values)
  {
      $err = '';
      foreach($values as $rows)
      {
          $clientQuery = 'INSERT INTO
                              client
                              (
                                  admin_id,
                                  create_time
                              )
                              VALUES
                              (
                                  "'.$this -&gt; session -&gt; userdata('user_id').'",
                                  "'.date('Y-m-d H:i:s').'"
                              )';
          $clientResult = @$this -&gt; db -&gt; query($clientQuery);
          if($clientResult)
          {
              $client_id = $this -&gt; db -&gt; insert_id();
              foreach($rows as $row)
              {
                  $attrQuery = 'INSERT INTO
                                      client_attribute_value
                                      (
                                          attribute_id,
                                          client_id,
                                          value
                                      )
                                      VALUES
                                      (
                                          "'.$row['attribute_id'].'",
                                          "'.$client_id.'",
                                          "'.addslashes(trim($row['value'])).'"
                                      )';
                  $attrResult = @$this -&gt; db -&gt; query($attrQuery);
                  if(!$attrResult)
                  {
                      $err .= '&lt;p class="box error"&gt;Could not add attribute for&lt;br&gt;
                              Attribute ID: '.$row['attribute_id'].'&lt;br&gt;
                              Client ID: '.$client_id.'&lt;br&gt;
                              Attribute Value: '.trim($row['value']).'&lt;/p&gt;';
                  }
              }
          }
      }
      return $err;
  }
  ?&gt;
  </code></pre>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>                $attrQuery = "INSERT INTO client_attribute_value (attribute_id, client_id, value) VALUES ";
  
                  $vls = array();
                  foreach($rows as $row) {
                      $myattribute_id = $row['attribute_id'];
                      $myclient_id = $row[$client_id];
                      $myvalue = addslashes(trim($row['value']));
                      $vls[] = " ( '$myattribute_id ', '$myclient_id ', '$myvalue ')";
  
  
                    $attrQuery .= implode(', ', $vls);
  
                    $attrResult = @$this -&gt; db -&gt; query($attrQuery);
  </code></pre>
  
  <p>Client Table sample:
  <img src="https://i.stack.imgur.com/rgHDA.jpg" alt="client table sample"></p>
  
  <p>client_attribute_value Table sample:
  <img src="https://i.stack.imgur.com/J4hmq.jpg" alt="client_attribute_value"></p>