Re: What's faster? MySQL Queries or PHP Loops?
| From: | Galen | Date: | Wed, 08 Sep 2004 16:46:31 +0000 |
| Subject: | Re: What's faster? MySQL Queries or PHP Loops? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196145@lists.php.net to get a copy of this message | ||
Almost all the time (unless the data being worked with is extremely, extremely large) PHP is much faster at manipulating data than MySQL. So if you already have the record set pulled out of MySQL, you probably want to go with PHP to manipulate it. Especially for repeating like this, there is zero reason to be querying MySQL excessively. Even if you have a huge chunk of data, I'd pull a big chunk of the records at a time and only query MySQL every 200 KB or something.
You still may want to check the performance (never a bad idea to find those slow parts of your code), so use a simple function like this to benchmark:
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
// Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>
(from http://php.net/microtime )
-Galen
On Sep 8, 2004, at 8:22 AM, Robb Kerr wrote:
Here's the scenario... First, my HTTP Server (Apache), PHP Server and MySQL Server are on the same machine - an Apple Xserve. Second, I've got a page with a long repeat region reflecting a recordset queried out of a MySQL table. The region also displays information obtained from fields in a related table. Third, I use Dreamweaver to generate my MySQL recordsets and repeat regions. Here's the question... I can either A) in the header or my page, generate a recordset of all of the records in the related table and then loop through the recordset creating an array of the fields I need and then later pull from it in the repeat region... or B) take the six lines of code Dreamweaver generates to create a recordset and move them into the repeat region itself. In other words, I can create a recordset of all of the records in the related table, loop through it generating a PHP array and pull from this array later OR I can query the database every time through the loop while creating the repeat region. Since I haven't freed the table until the bottom of the page and because my MySQL Sever and PHP Server reside on the same machine, will I really notice a measurable difference in speed? If my MySQL Server were a different machine, I'm sure that there would be a noticable difference because all of the queries would be across a network (possibly the internet) and traffic would become a factor. Just wondering what other people have noticed. -- Robb Kerr Digital IGUANA Helping Digital Artists Achieve their Dreams ---------------------------------------------------- http://www.digitaliguana.com http://www.cancerreallysucks.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php