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.
//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<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 '<br/>'. ($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;
}
EDIT: The $time_start
and $time_end
variables are ONLY for performance testing purposes. Also the MySQL table has two fields only: ID int(11) UNSIGNED NOT NULL AUTO_INCREMENT
and sha1_hash varchar(48) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
, the engine is MyISAM
EDIT2: The computer hardware point of view is not related to the question.