title: Optimized insert in chuncks of millions of records, MySQL and PHP
body: <p>I need to populate a MySQL table with random SHA-1 hash values, generated by PHP function. 
  I`m trying to optimize the insert by splitting it in chunks of 10000.
  My question is:
  Is the following approach efficient? Here is the code.</p>
  
  <pre><code>//MySQL server connection routines are above this point
  if ($select_db) {
  $time_start = microtime(true);
  //query
  $query = 'INSERT INTO sha1_hash (sha1_hash) VALUES ';
  for ($i=1; $i&lt;1000001; $i++) {
   $query .= "('".sha1(genRandomString(8))."'),";
      $count++;
      if ($count ==10000) {
      //result
   $result = mysql_query(rtrim($query,',')) or die ('Query error:'.mysql_error());
      if ($result) mysql_free_result($result);
      $count = 0;
      }
  }
  
  $time_end = microtime(true);
  echo '&lt;br/&gt;'. ($time_end - $time_start);
  }
  
  //function to generate random string
  function genRandomString($length)
  {
  $charset='abcdefghijklmnopqrstuvwxyz0123456789';
  $count = strlen($charset);
   while ($length--) {
    $str .= $charset[mt_rand(0, $count-1)];
   }
  return $str;
  }
  </code></pre>
  
  <p>EDIT: The <code>$time_start</code> and <code>$time_end</code> variables are ONLY for performance testing purposes. Also the MySQL table has two fields only: <code>ID int(11) UNSIGNED NOT NULL AUTO_INCREMENT</code> and <code>sha1_hash varchar(48) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL</code>, the engine is <code>MyISAM</code>
  EDIT2: The computer hardware point of view is not related to the question.</p>