title: Codeigniter's insert_batch() with thousands of inserts has missing records
body: <p>I’m using <code>insert_batch()</code> to mass insert 10000+ rows into a table in a database. I’m making some tests and I have noticed that sometimes all the 10.000+ rows are getting inserted correctly but in some occasions I miss 100+ rows in my table’s total count.</p>
  
  <p>The field data in the records I have are ok as I'm using the same data for each of my tests and most of the times I have no problem. For example I tried 20 times to insert the same data into my database and at 19 times all rows will be inserted correctly but in this one time I will miss 100 or maybe more rows.</p>
  
  <p>The function for the <code>insert_batch()</code> follows:</p>
  
  <pre><code>protected function save_sms_to_database() {
      //insert_Batch
      $datestring = "%Y-%m-%d %h:%m:%s";
      $time = time();
      $datetime = mdate($datestring, $time);
  
      $this-&gt;date_sent = $datetime;
  
      foreach ($this-&gt;destinations as $k =&gt; $v) {
          $sms_data[$k] = array(
              'campaign_id' =&gt; $this-&gt;campaign_id,
              'sender_id' =&gt; $this-&gt;from,
              'destination' =&gt; $v,
              'token' =&gt; md5(time() . 'smstoken' . rand(1, 99999999999)),
              'message' =&gt; $this-&gt;body,
              'unicode' =&gt; $this-&gt;unicode,
              'long' =&gt; $this-&gt;longsms,
              'credit_cost' =&gt; $this-&gt;eachMsgCreditCost,
              'date_sent' =&gt; $this-&gt;date_sent,
              'deleted' =&gt; 0,
              'status' =&gt; 1,
              'scheduled' =&gt; $this-&gt;scheduled,
          );
      }
  
      $this-&gt;ci-&gt;db-&gt;insert_batch('outgoingSMS', $sms_data);
      if ($this-&gt;ci-&gt;db-&gt;affected_rows() &gt; 0) {
          // outgoingSMS data were successfully inserted      
          return TRUE;
      } else {
          log_message('error', $this-&gt;campaign_id.' :: Could not insert sms into database');
          log_message('error', $this-&gt;ci-&gt;db-&gt;_error_message());
          return FALSE; // sms was not inserted correctly
      }
  }
  </code></pre>
  
  <p>How can I debug <code>insert_batch()</code> for such an occasion?</p>
  
  <p>I have made some changes on the <code>DB_active_rec.php</code> to do some logging during the insert_batch and so far I can’t successfully reproduce the problem to see what is going wrong. But as far as the problem appeared 2-3 times at the beginning and I did not major changes to my logic to fix it, I can’t leave it like this as I don’t trust codeigniter’s <code>insert_batch()</code> function for production.</p>
  
  <p>I'm also adding codeigniter's insert_batch() function:</p>
  
  <pre><code>    public function insert_batch($table = '', $set = NULL)
  {
          $countz = 0;
      if ( ! is_null($set))
      {
          $this-&gt;set_insert_batch($set);
      }
  
      if (count($this-&gt;ar_set) == 0)
      {
          if ($this-&gt;db_debug)
          {
              //No valid data array.  Folds in cases where keys and values did not match up
              return $this-&gt;display_error('db_must_use_set');
          }
          return FALSE;
      }
  
      if ($table == '')
      {
          if ( ! isset($this-&gt;ar_from[0]))
          {
              if ($this-&gt;db_debug)
              {
                  return $this-&gt;display_error('db_must_set_table');
              }
              return FALSE;
          }
  
          $table = $this-&gt;ar_from[0];
      }
  
      // Batch this baby
      for ($i = 0, $total = count($this-&gt;ar_set); $i &lt; $total; $i = $i + 100)
      {
  
          $sql = $this-&gt;_insert_batch($this-&gt;_protect_identifiers($table, TRUE, NULL, FALSE), $this-&gt;ar_keys, array_slice($this-&gt;ar_set, $i, 100));
  
          //echo $sql;
  
          $this-&gt;query($sql);
                      $countz = $countz + $this-&gt;affected_rows();
      }
  
      $this-&gt;_reset_write();
              log_message('info', "Total inserts from batch:".$countz);
  
      return TRUE;
  }
  </code></pre>
  
  <p>The last <code>log_message()</code> with the total inserts from batch also shows the problem as when I have less inserts than expected I get the non-expected number of inserts there as well. </p>
  
  <p>I have to think something else for inserting thousands of rows into my database w/ or w/o codeigniter.</p>
  
  <p>anyone has any clue for this kind of problem? Maybe it has something to do with the hard drive or the memory of the system during to lack of performance? It's an old PC with 1gb of ram.</p>
  
  <p>EDIT: As requested I'm putting here an example INSERT statement with 9 rows that is being produced by codeigniter's <code>insert_batch()</code> function</p>
  
  <pre><code>INSERT INTO `outgoingSMS` (`campaign_id`, `credit_cost`, `date_sent`, `deleted`, `destination`, `long`, `message`, `scheduled`, `sender_id`, `status`, `token`, `unicode`) VALUES ('279',1,'2013-08-02 02:08:34',0,'14141415151515',0,'fd',0,'sotos',1,'4d270f6cc2fb32fb47f81e8e15412a36',0), ('279',1,'2013-08-02 02:08:34',0,'30697000000140',0,'fd',0,'sotos',1,'9d5a0572f5bb2807e33571c3cbf8bd09',0), ('279',1,'2013-08-02 02:08:34',0,'30697000000142',0,'fd',0,'sotos',1,'ab99174d88f7d19850fde010a1518854',0), ('279',1,'2013-08-02 02:08:34',0,'30697000000147',0,'fd',0,'sotos',1,'95c48b96397b21ddbe17ad8ed026221e',0), ('279',1,'2013-08-02 02:08:34',0,'306972233469',0,'fd',0,'sotos',1,'6c55bc3181be50d8a99f0ddba1e783bf',0), ('279',1,'2013-08-02 02:08:34',0,'306972233470',0,'fd',0,'sotos',1,'d9cae1cbe7eaecb9c0726dce5f872e1c',0), ('279',1,'2013-08-02 02:08:34',0,'306972233474',0,'fd',0,'sotos',1,'579c34fa7778ac2e329afe894339a43d',0), ('279',1,'2013-08-02 02:08:34',0,'306972233475',0,'fd',0,'sotos',1,'77d68c23422bb11558cf6fa9718b73d2',0), ('279',1,'2013-08-02 02:08:34',0,'30697444333',0,'fd',0,'sotos',1,'a7fd63b8b053b04bc9f83dcd4cf1df55',0)
  </code></pre>
  
  <p>That was a completed insert.</p>