我正在运行一个PHP脚本,它基本上试图从MYSQL数据库中找到匹配的名称,然后将相同的编号分配给具有相同名称的记录。
我的问题是,纪录的数目约为150万。该脚本每次运行约14个小时,并给出以下错误: mysql_query无法将结果集保存在xx上的line中。而phpmyadmin给出了这个错误#2008出于记忆
这是我的php代码
mysql_query("SET SQL_BIG_TABLES=1");
$res = mysql_query("SELECT company_name, country, id FROM proj")
or die (mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
$res1 = mysql_query("SELECT company_name, id FROM proj WHERE country='$row[1]'"+
"AND id<>'$row[2]'") or die ("here".mysql_error().mysql_errno());
while ($row1 = mysql_fetch_array($res1, MYSQL_NUM)) {
//My calculations here
}
}发布于 2011-04-17 05:10:39
好的。您的查询效率极低。你在评论中说有150万行。
换句话说,这不仅是令人难以置信的低效--这是绝对可笑的。如果您似乎只想按国家提取行,为什么不这样做:
$sql1 = "SELECT DISTINCT country FROM proj";
$res1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_associ($res1)) {
$country = $row1['country'];
$escaped_country = mysql_real_escape_string($country);
$sql2 = "SELECT company_name, id FROM proj WHERE country='$country'";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
... calculations ...
}
}这只会从数据库中获取150万+#的国家记录,这远远低于你的版本的2.3万亿。
https://stackoverflow.com/questions/5691262
复制相似问题