After using php and mysql for 10+ years I thought I knew every optimisation trick there is.
But yesterday I found something I had never heard of and using it made mysql / php reports run 15% faster! Reports that took 12 seconds now take 10, 6 seconds now take 5.
The function is
mysql_unbuffered_query() Using it did give a significant boost to my code. And it's so simple to use - just replace (some) existing mysql_query() functions with mysql_unbuffered_query()
It will not work in all situations and there are some disadvantages. But if you tend to write code which looks similar to this:
$result = mysql_unbuffered_query("SELECT lots_of_fields from big_table" );
while ($row = @mysql_fetch_assoc($result)) {
//loop through processing and displaying data
}
Then you too will see a big change in performance.
Advantages Big boost in performance because data available immediately (not buffered)
Uses less memory (only one row a time is stored in RAM)
Disadvantages You have to read all the data and cannot issue another select within the loop
You can not use mysql_num_rows to determine how many rows returned
Table will be locked whilst you loop through
Some of the disadvantages can be overcome but this function works best if you have static tables, which are not updated often.
I copy my tables to ENGINE=MEMORY and run them from there. That is the fastest way to read MyISAM tables. By now using mysql_unbuffered_select() I got an extra 15% out of it.
It's amazing that after all these years you discover something in the attic that you never knew about, but which makes such a change!