Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.2     Use your tools wisely

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

At various times through my PHP career, I have seen pages with sections of code like this:

$result = mysql_query("SELECT * FROM Users;");
while (
$row = mysql_fetch_assoc($result)) {
    if (
$row['ID'] == '3') {
        
// do stuff here
    
}
}

As you can see, they are using PHP to filter their SQL results - looking through the entire result set for a particular row.

SQL is designed to extract specific data from a database, and you should exploit its power as much as possible. MySQL is blazingly fast at processing queries, and modifying the above code to the following would yield an exponential speed increase:

$result = mysql_query("SELECT * FROM Users WHERE ID = 3;");
while (
$row = mysql_fetch_assoc($result)) {
    
// do stuff here
}

For very complicated SQL manipulation, you should use temporary tables - more information can be found on temporary tables in the Databases chapter.

A similar problem exists when people use functions that are more complex than is required for the task. For example, preg_replace() is a powerful way to search and replace text in a string, but if you are not doing regular expressions you should be using str_replace() as it's much faster. Similarly, explode() is a better choice than preg_split() when regular expressions are not required.





<< 18.1.1 Write your code sensibly   18.1.3 Avoid function for maximum performance >>
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
Cursed - 06 Sep 2008

...and "SELECT a,b,c FROM users WHERE id=33 LIMIT 1" would stop searching as soon it finds matching row..

Of course, this only works when we know that there are only 1 amtching row at most.

DiamondHeart - 06 Sep 2008

...and "select *" is slower than "select a, b, c,..."



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 two plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow