Hudzilla.org - the homepage of Paul Hudson
Contents > The future of PHP Wish List | Report Bug | About Me ]

25.3     Optimisation summary

This is NOT the latest copy of this book; click here for the latest version.

The optimisation chapter ended up being a lot longer than I planned, which in turn means you can't just flick through it and make the appropriate changes to your script! So, I've summarised the various optimisation hints and tips here; for more information on any of these, please go back to the Optimisation chapter.

First up, here are the PHP optimisations:

  • Use the Zend Optimizer and a code cache

  • Use the comma operator to join arguments when calling echo

  • Priorities optimisation of tight loops for the most payback

  • Pre-increment where possible

  • Don't rely on references without testing; they are rarely as effective as you'd think

  • Get a good understanding of how garbage collection works

  • Listen to all errors, big and small - they slow things down

  • Store a local pointer to an array element to save indexing into the array each time you need it

  • Compress your output to save network bandwidth

  • Try to avoid using PHP in CGI mode

  • Avoid calling the dl() function

  • Use persistent resource where appropriate

  • Optimise compilation of your PHP binary if possible

  • Avoid mod_access if you can

  • Don't fret about Apache 2.0

  • As a last resort, try inlining functions

Next, the SQL optimisations:

  • Prioritise your queries as LOW_PRIORITY, HIGH_PRIORITY, or DELAYED when it matters

  • Don't use SELECT * unless you must

  • Use the EXPLAIN keyword to see how MySQL will execute your query - make sure your indexes are being used!

  • Load your data before adding indexes is faster than adding indexes first

  • Be wary of locks

  • Use --log-slow-queries to see where your tables can be optimised

  • Increase your buffer sizes so that MySQL can cache more

  • Use SHOW STATUS to make sure your MySQL server is in good condition

  • Don't create indexes you aren't going to use

  • Do as much as your filtering in SQL as you can

  • Indexes are good for reads, but bad for writes

  • Use OPTIMIZE TABLE and ANALYZE TABLE regularly

  • Create your tables with a fixed-table format if possible

  • Use the most efficient table type for each table

  • Use the best data type, including NOT NULL if appropriate - don't rely on automatic type conversion

  • Use default values for INSERT when you can

  • Use temporary tables rather than heavy PHP work

  • SELECT foo IN (list, of, constants) is very fast

  • When joining tables, use numbers instead of strings if you can

Finally, here are the server-related tips:

  • Having a dual-CPU machine (or more) makes an enormous difference

  • Don't worry about SSE, Hyperthreading, or 64-bit for Apache web servers, but do consider 64-bit for your MySQL server

  • Try to get the fastest RAM available: 533MHz RAM is highly recommended

  • Most data should be cached in RAM, but don't skimp on hard disks just in case - get S-ATA at the very least

  • Resist moving from one machine to two until it becomes financially necessary

  • When you network, Gigabit Ethernet is essential, preferably with each machine connected directly to a switch

  • Using a round robin DNS system is a simple load-balancing system, but don't forget to put session data in a database

  • Blade servers are a cheap and efficient solution for web server systems, but not for SQL servers.

  • If you are able, set up one machine as a reverse proxy for your site.





<< 25.2.1 Does your ISP support PHP?   26 Glossary >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow