Re: msql perfomance: fast through mysql, slow through php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There are couple of things that look like they may be slowing you down. The first one being the east coast/west coast thing. There's a big possibility for latency issues going that distance no matter how fast your connection is. You can do a quick test using ping.

You're also writing out to "screen" as you get the data, so you may be adding the latency from the web server to the client on top of your data fetching. You can probably get a boost by getting all the data into PHP first and then formatting and sending it to the client. At a minimum you should be able to determine if the client is the big slow down.

This is the standard code (simplified a bit) I use for getting data from MySQL.

$query = "select acolumn from atable where bcolumn=0";
$result = mysql_query($query,$db);
if($result) {
	$num_rows = mysql_num_rows($result);
	while ($row = mysql_fetch_array ($result,MYSQL_ASSOC)) {
		$result_arr[] = $row;
	}
}

Now all your data is being held by PHP in the array variable $result_arr, MySQL is out of the picture and the client is not yet part of the picture. You then cycle through the array, formatting the output for the client. Ideally, you would also use output buffering in PHP and then deliver the final formatted output in one big chunk to the client rather than "streaming" it a little bit at a time. And if you are using output buffering, you can then using compression to deliver the data to the client for an even bigger speed boost, especially if the client is connected over a modem.
Look into ob_start("ob_gzhandler") for details on using output buffering.


Hope some of those suggestions speed things up.

On Feb 8, 2004, at 10:47 PM, jde wrote:

Greetings,

I have search the archives over the last two years and have not found any help for this question.

The basic setup: An apache/php server exists on the west coast of the US, and a linux/mysql server on the east coast.

My PHP snippet looks like this:

$result = mysql_query("select acolumn from atable where bcolumn=0", $db);
while ($row = mysql_fetch_assoc($result)) echo 'some stuff from the row';



The problem is, that the above snippet, to return seven rows, takes about 15 seconds. Putting timing statements around the query and around the fetch calls shows that the query call takes only 0.03 seconds. However, each fetch call takes anywhere from 0.5 to 5 seconds each to complete.


However, if I am logged onto the apacher server and from the user prompt run the 'mysql' client, and perform the same exact query, the 'mysql' client can peform the query and return all the results, all 7 rows, in 0.1 second.

This is apache 1.3/php 4.3.4 and mysql 4.0.17-standard.

I have tried all the mysql_connect flags without any change.

Should I expect such a performance difference? Is there something else I need to look for?

Thank you for your time,
jde

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux