PHP4/5 MySQL nested query problem
| From: | Kevin Wormington | Date: | Sun, 22 Aug 2004 23:29:50 +0000 |
| Subject: | PHP4/5 MySQL nested query problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-194435@lists.php.net to get a copy of this message | ||
I have ran into an interesting query performance problem that occurs with both 4.3.7 and 5.0.1 against a MySQL 4.1 database. The test program (attached below) works, but performance is extremely sloooow, a perl version of the test program completes in less than 5 seconds with 80k records in table 1 and 138k records in table 2. The PHP program is taking something like 1 to 2 seconds per record. This only seems to occur when you have a query nested inside of another. My test program attached uses mysqli, but mysql functions yield the same result. Any ideas would be appreciated.
<?
$link = mysqli_connect("localhost","test","test","db");
$qry = "select a,b from table1";
$result = mysqli_query($link,$qry);
if (!$result) { echo mysql_error() . "\n"; }
while ($data = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$sql = "select * from table2 where common=".$data[0];
$result2 = mysqli_query($link,$sql);
if (!$result2) { echo mysql_error() . "\n"; }
while ($data2 = mysqli_fetch_array($result2,MYSQLI_BOTH)) {
$linenum++;
}
mysqli_free_result($result2);
}
mysqli_free_result($result);
mysqli_close($link);