【发布时间】:2012-05-16 01:10:25
【问题描述】:
我需要用 PHP 函数生成的随机 SHA-1 哈希值填充 MySQL 表。 我正在尝试通过将其拆分为 10000 个块来优化插入。 我的问题是: 以下方法有效吗?这是代码。
//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;
}
编辑:$time_start 和 $time_end 变量仅用于性能测试目的。 MySQL表也只有两个字段:ID int(11) UNSIGNED NOT NULL AUTO_INCREMENT和sha1_hash varchar(48) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,引擎是MyISAM
EDIT2:计算机硬件的观点与问题无关。
【问题讨论】:
-
注意 MySQL 管理设置中允许的最大数据包大小:构建非常长的查询(正如数千次迭代所暗示的那样)可能会超过该限制。
-
在什么情况下有效?总执行时间?内存使用情况?系统负载?
-
Wally,在所有提到的方面都很有效
-
确实,允许的最大数据包大小已增加到 1024 Mb,最低点
标签: php mysql performance performance-testing mysql5