简体   繁体   中英

MYSQL/PHP Unable to save result set

I am running a PHP script which basically tries to find matching names from MYSQL database and then assigns the same number to records with identical names.

My problem is that the number of records are around 1.5 million. The script always runs for about 14 hours every time and gives this error : mysql_query unable to save result set in xx on line xxx. and phpmyadmin gives this error #2008 out of memeory

Here is my php code

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 
    }
}

Ok. Your query is incredibly inefficient. You say in a comment that there's 1.5 million rows.

  1. Outside query creates a result set with all 1.5 million rows
  2. Inside query creates a new result set with all 1.5 million rows, EXCEPT the row that has the same of the row you're looping on. So basically you're doing 1.5 million * (1.5 million - 1) rows = 2,249,998,500,000 = 2.25 trillion rows

In other words, not only incredibly inefficient - it's absolutely ludicrous. Given you seem to want to fetch only rows by country, why not do something like:

$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 ...
    }
}

This'd fetch only 1.5 million + # of country records from the database, which is far far far less than the 2.3 trillion your version has.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM