title: why Zend_Db_Adapter is much slower than mysql_query
body: <p>I use Zend Framework in my project. I need to insert multiple records and I found that Zend_Db suprisingly slower thatn my_sql query (several times), that made me think I did something wrong.
  Here are two examples.</p>
  
  <p>Zend_Db_Adapter:</p>
  
  <pre><code>        $startTime = microtime(true);
          $db = Zend_Db_Table::getDefaultAdapter();
          $db-&gt;beginTransaction();
  
          $dateAdded = date('Y-m-d H:i:s');
          $lastChanged = $dateAdded;                  
  
          foreach ($importDataNamespace-&gt;data as $subscriberNum =&gt; $subscriber)
          {
              foreach ($fieldsMap as $fieldNumber =&gt; $fieldTag) {
                  if (isset($subscriber[$fieldNumber])) {
                      $subscriberData[$fieldTag] = $subscriber[$fieldNumber]; 
                  } else {
                      $subscriberData[$fieldTag] = '';
                  }
              }
              $query = 'INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) ' .
                       'VALUES (' . 52 . ', ' . 29 . ', ' .  $db-&gt;quote($subscriberData['EMAIL']) . ', ' . $db-&gt;quote($subscriberData['FNAME']) .
                       ', ' . $db-&gt;quote($subscriberData['LNAME']) . ', ' . $db-&gt;quote($dateAdded) . ', ' . $db-&gt;quote($lastChanged) . ')';
              $db-&gt;query($query);                                                                 
          }
          $db-&gt;commit();
  
          $this-&gt;view-&gt;time = microtime(true) - $startTime;
  </code></pre>
  
  <p>Example with mysql_query:</p>
  
  <pre><code>        $startTime = microtime(true);
  
          $user = 'root';
          $password = 'password';
          $db = 'database';
          $connect = @mysql_connect('localhost',$user,$password) or die("Failed to connect database");
          @mysql_select_db($db) or die("Failed to select database");          
  
          $dateAdded = date('Y-m-d H:i:s');
          $lastChanged = $dateAdded;      
  
          $result = mysql_query('SET autocommit = 0');            
  
          foreach ($importDataNamespace-&gt;data as $subscriberNum =&gt; $subscriber)
          {
              foreach ($fieldsMap as $fieldNumber =&gt; $fieldTag) {
                  if (isset($subscriber[$fieldNumber])) {
                      $subscriberData[$fieldTag] = $subscriber[$fieldNumber]; 
                  } else {
                      $subscriberData[$fieldTag] = '';
                  }
              }
              $query = 'INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) ' .
                  'VALUES (' . 52 . ', ' . 29 . ', \'' .  mysql_real_escape_string($subscriberData['EMAIL']) . '\', \'' . mysql_real_escape_string($subscriberData['FNAME']) .
                  '\', \'' . mysql_real_escape_string($subscriberData['LNAME']) . '\', \'' . $dateAdded . '\', \'' . $lastChanged . '\')';                    
              mysql_query($query);                                    
          }
          $result = mysql_query('SET autocommit = 1');
          $result = mysql_query('COMMIT;');
  
          $this-&gt;view-&gt;time = microtime(true) - $startTime;           
  </code></pre>
  
  <p>In the first case it took 14.8 seconds, in the second 3.7.
  Could you tell me why does it happend and what do you do wrong?</p>
  
  <p>If I delete any quote for Zend_Db it took 12 seconds from 14 with quote, but it's still much more slower than with mysql_query:</p>
  
  <pre><code>        $startTime = microtime(true);
          $db = Zend_Db_Table::getDefaultAdapter();
          $db-&gt;beginTransaction();
  
          $dateAdded = date('Y-m-d H:i:s');
          $lastChanged = $dateAdded;                  
  
          foreach ($importDataNamespace-&gt;data as $subscriberNum =&gt; $subscriber)
          {
              foreach ($fieldsMap as $fieldNumber =&gt; $fieldTag) {
                  if (isset($subscriber[$fieldNumber])) {
                      $subscriberData[$fieldTag] = $subscriber[$fieldNumber]; 
                  } else {
                      $subscriberData[$fieldTag] = '';
                  }
              }
              $query = 'INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) ' .
                       'VALUES (' . 52 . ', ' . 29 . ', \'' .  $subscriberData['EMAIL'] . '\', \'' . $subscriberData['FNAME'] .
                       '\', \'' . $subscriberData['LNAME'] . '\', \'' . $dateAdded . '\', \'' . $lastChanged . '\')';
              $db-&gt;query($query);                                                         
          }
          $db-&gt;commit();
  
          $this-&gt;view-&gt;time = microtime(true) - $startTime;
  </code></pre>
  
  <p>Thank you for any information about this issue.</p>
  
  <p>This code takes about 0.065 seconds with mysql_query:</p>
  
  <pre><code>    $dateAdded = date('Y-m-d H:i:s');
      $lastChanged = $dateAdded;      
  
      $startTime = microtime(true);       
      $result = mysql_query('BEGIN');
      for ($i = 0; $i &lt; 100; $i++) {
          $email = 'test_ ' . $i . '@gmail.com';
          $query = 'INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) ' .
                  'VALUES (' . 52 . ', ' . 29 . ', \'' .  mysql_real_escape_string($email) . '\', \'' . mysql_real_escape_string($firstName) .
                  '\', \'' . mysql_real_escape_string($lastName) . '\', \'' . mysql_real_escape_string($dateAdded) . '\', \'' . mysql_real_escape_string($lastChanged) . '\')';                                                                   
          mysql_query($query);            
      }               
      $result = mysql_query('COMMIT');
      $time = microtime(true) - $startTime;                           
      echo 'Using mysql_query: ' . $time  . '&lt;br /&gt;';
      exit(); 
  </code></pre>
  
  <p>Code with benchmark of Zend_Db_Adapter (I didn't even use quote in this case):</p>
  
  <pre><code>    $db = Zend_Db_Table::getDefaultAdapter();
      $db-&gt;getProfiler()-&gt;setEnabled(true);
      $profiler = $db-&gt;getProfiler();          
  
      $startTime = microtime(true);
      $db-&gt;beginTransaction();        
      for ($i = 0; $i &lt; 100; $i++)
      {
  
          $email = 'test_' . $i . '@gmail.com';
          $query = 'INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) ' .
               'VALUES (' . 52 . ', ' . 29 . ', \'' . $email . '\', \'' . $firstName .
               '\', \'' . $lastName . '\', \'' . $dateAdded . '\', \'' . $lastChanged . '\')';
          $db-&gt;query($query);                                 
      }           
      $db-&gt;commit();
      $time = microtime(true) - $startTime;                       
      echo 'Time of transaction Zend_Db_Adapter query: ' . $time . '&lt;br /&gt;';
  
      echo 'Total time ' . $profiler-&gt;getTotalElapsedSecs() . '&lt;br /&gt;';           
      $count = 0;
      $totalTime = 0; 
      foreach ($profiler-&gt;getQueryProfiles() as $query) {
          $count++;
          $elapsedTime = $query-&gt;getElapsedSecs();
          $totalTime += $elapsedTime;
          echo $count . ' ' . $elapsedTime  . ' ' . $query-&gt;getQuery() . '&lt;br /&gt;';
      }
      echo 'Sum time: ' . $totalTime . '&lt;br /&gt;'; 
  </code></pre>
  
  <p>Here are some results:</p>
  
  <p>Time of transaction Zend_Db_Adapter query: 0.23094701767
  Total time 0.0949234962463
  1 0.00199699401855 connect
  2 0.000336885452271 begin
  3 0.000540018081665 INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) VALUES (52, 29, 'test_0@gmail.com', 'John', 'Clinton', '2011-01-28 15:25:21', '2011-01-28 15:25:21')
  4 0.000504016876221 INSERT INTO subscribers (list_id, account_id, email_address, first_name, last_name, date_added, last_changed) VALUES (52, 29, 'test_1@gmail.com', 'John', 'Clinton', '2011-01-28 15:25:21', '2011-01-28 15:25:21')</p>
  
  <p>It's very strange. The time of transacation to insert 100 records is 2.5 times more than executing of all queries.
  If I try to meach the time of forming strings in the loop it (if we delete query) doesn't take so much time. </p>