View Single Post
Old 05-22-2009, 09:34 PM   #5 (permalink)
sketchMedia
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
I've read somewhere that using SELECT * FROM causes too much CPU usage, so even if it is a pain, I suppose you can make a SELECT call on each column, this way mysql doesn't have to think.
Indeed! You should always be careful when using SELECT *.

You need to ask yourself: Do I need all of the columns? more than likely not.

The reason you need to be wary is that getting MySQL to retrieve data it doesn't need is a waste of resources better used elswhere. This waste adds disk I/O, memory and CPU overheads where they are simply not needed, not to mention network overhead and also potentially b0rking up covering indexes (if there are any).

Quote:
You can probably get away with using mysql_fetch_assoc($result) instead of array, I don't know if it's true, but I heard it was a little faster too...
This may be true (haven't tested it) because mysql_fetch_array by default will return both numeric AND associative arrays for the result (unless you pass in the 2nd param), where as mysql_fetch_assoc will only return an associative array.

Quote:
Also, to replace the if's, you can use switch, it is faster.
Whilst I do believe this to be true (it also adds a heap of readability to long conditional statements), this post suggests otherwise: http://www.fluffycat.com/PHP-Design-...-if-VS-switch/

N.B read the comments, its quite intriguing, I'll have to run my own benchmarks.

This brings me nicely to my next point, if a script needs so many if/else's there must be an easier way to do it.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 05-22-2009 at 09:45 PM. Reason: fixed some crappy engrish
sketchMedia is offline  
Reply With Quote