The San Francisco PHP Meetup Group Message Board › Took long time and occupied 10MB memory to display result while iterating th
| Vijay Joseph | |
|
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 | |
|
Can you post table structure? Is you database on a separate server? What are the specs?
|
|
| Danny Wilkerson II | |
|
|
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. |