PHP is very slow when printing large amounts of information

I have an application where I need to get a large number of rows from a database, and then print them all on the screen. I checked mysql queries and not where the problem is. The problem is that all lines should be printed on one page, without pagination, and this takes a lot of time (I'm talking about a table with several thoudands of lines). Is there a way to speed up the process? The only thing I found on Google is "," instead of ".". when using an echo. I will test this to see if there are any improvements, but I'm not sure if this will make a big difference.

+2
source share
2 answers

The problem is unlikely in PHP, it is a huge amount of HTML that is displayed in the browser. You can verify this yourself by saving the page that you get, and then download the static file from your hard drive.

If you display a large table, the browser will often have problems redrawing it as the content loads. This can be minimized by setting a fixed width for all columns. Just do it on the first line.

You can also jump out of the PHP block to output most of the HTML, and then simply print the variables where necessary. Here's an example, assuming all your data is in the $rows variable:

 <?php // some code here ?> <table> <thead> <tr> <th width="50">Header 1</th> <th width="200">Header 2</th> <th width="150">Header 3</th> <th width="100">Header 4</th> </tr> </thead> <tbody> <?php foreach ( $rows as $r ) : ?> <tr> <td><?=$r['var1']?><td> <td><?=$r['var2']?><td> <td><?=$r['var3']?><td> <td><?=$r['var4']?><td> </tr> <?php endforeach; ?> </tbody> </table> <?php // continue here if necessary 

Note. If you don't have short PHP tags, you need to do <?php echo $r['var1'] ?> Instead.

Finally, you can try adding gzip compression. This is best done at the server level, but in PHP you can add the string ob_start("ob_gzhandler"); like the very first line of PHP code right after <?php .

If this still does not help, then it is a simple fact that your table is too large. You will be much better off using pagination or filtering to reduce the size.

+5
source

PHP is not your bottleneck. Here is a presentation in which I participated in PHP performance from Rasmus Lerdorf, the guy who created PHP. PHP is one of the last things you need to worry about in terms of performance. You can print millions of lines and perform complex calculations without any problems. Before throwing it into PHP, check your code, your requests, your web server settings.

As Mark noted, you can use flush to send output to the browser while still receiving information from db.

+2
source

All Articles