The San Francisco PHP Meetup Group Message Board › Took long time and occupied 10MB memory to display result while iterating th

Took long time and occupied 10MB memory to display result while iterating through database result set. (PHP/MySql)

Vijay Joseph
Posted Sep 21, 2009 2:27 AM
pjcvijay
Chennai, IN
Post #: 1
Send an Email Post a Greeting
Dear Friends,

I execute the following scripts, it took almost 10 minutes and 10MB Memory to display the contents. I would like to solve this problem without using too much memory and long delay. 'SELECT * from employees_salary WHERE emid=1' will return 6015 records. Please help me to solve this problem.

$link=mysql_connect('localhost','root','');
if(!$link){
die("Not connected".mysql_error());
}
$db=mysql_select_db('auto3',$link);
if(!$db) {
die("DB Does not Exits".mysql_error());
}
$sql = 'SELECT * from employees_salary WHERE emid=1';
$result = mysql_query($sql);

while ($row=mysql_fetch_row($result)) {
echo '<pre>';
print_r($row);
echo '</pre>';
}
}

Thanks in advance,
Vijay
Geo
Posted Sep 30, 2009 3:14 PM
user 10534703
San Francisco, CA
Post #: 1
Send an Email Post a Greeting
Can you post table structure? Is you database on a separate server? What are the specs?
Danny Wilkerson II
Posted Nov 9, 2009 10:35 PM
user 7857566
Dallas, TX
Post #: 78
Send an Email Post a Greeting
1) use LIMIT <number> so that you don't get all of them at once and display them as pages to a user.
2) instead of '*' use the names of the fields you want to eliminate unnecessary returned data to go through.
3) use mysql_fetch_array and put all the results into 1 array and then you only need 1 call to '<pre>'.print_r($rows).'</pre>';
4) cache the results. Get the array, seralize and save someplace so next time, you don't have to query database.
5) add a timestamp column and make it a second key, load in results of old query saved by seralize/session then do query for only records that have been changed recently and then update the origional array.
6) if this is only for admin and database is at remote location, you could try using sqlite inorder to bypass slow mysql server
7) if database is mostly for data retrival, make sure it is using MyISAM instead of transaction safe InnoDB which is slower
8) use CHARACTER SET utf8 so php and mysql do not start disagreeing and you have to do manual conversions latter. All you webpages should be utf-8 anyway.
9) use ob_start() and save resulting html so next time person views page it will be fast

I can't come up with anything else. Maybe some of the SQL experts could chime in and help you create more advances queries.
Powered by mvnForum

Our Sponsors

GameSpot.com

GameSpot.com & CBSi provides the facilities our group uses every month.

O'Reilly User Group Program

The O'Reilly User Group Program provides the great books we get monthly.

Other nearby
Meetup Groups
Why these groups?
x

The Meetup Groups shown here are topically similar to The San Francisco PHP Meetup Group.

Groups are more likely to be displayed here if they:

  • have a Meetup scheduled
  • have a high rating
  • have a group photo
  • are "public" and not "private"
  • have shown they are likely to stick around (older than 30 days)