title: Phalcon bulk insert performance. Models or Raw SQL
body: <p>Is there a way to do bulk inserts in phalcon php?</p>
  
  <p>I tried inserting 10000 records using <code>\Phalcon\Mvc\Model create</code> and also the same using raw SQL: <code>insert into 'tablename' (a, b, c) values (), (), (), ... ()</code></p>
  
  <p>I set up this very simple test, and was surprised by the results</p>
  
  <ul>
  <li>using \Phalcon\Mvc\Model took around 36 seconds</li>
  <li>using raw sql took 3,6 seconds.</li>
  </ul>
  
  <p>\Phalcon\Mvc\Model:</p>
  
  <pre><code>$transactionManager = new TransactionManager(); 
  
  $transaction = $transactionManager-&gt;get();
  
  $entity = new Entity();
  $entity-&gt;setTransaction($transaction);
  for($i = 0; $i &lt; 10000; $i++){
      $entity-&gt;setValue1(rand(1,50));
      $entity-&gt;setValue2(rand(1,50));
      $entity-&gt;setValue3(rand(1,50));
  }
  $entity-&gt;create();
  $transaction-&gt;commit();
  </code></pre>
  
  <p>Raw SQL: </p>
  
  <pre><code>$query = "insert into 'tablename' (a, b, c) values ";
  
  for($i = 0; $i &lt; 10000; $i++){
      $values .= "(" . rand(1,50) . ", " . rand(1, 50). ", " . rand(1, 50). "),";
  }
  
  $values = substr($values, 0, strlen($values) - 1);
  $query .= $values;
  $this-&gt;db-&gt;query($query);
  </code></pre>