Optimization of some code!
View Single Post
05-22-2009, 09:34 PM
Join Date: Oct 2007
Location: Manchester, UK
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).
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
by default will return both numeric AND associative arrays for the result (unless you pass in the 2nd param), where as
will only return an associative array.
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:
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
. Reason: fixed some crappy engrish
View Public Profile
Send a private message to sketchMedia
Visit sketchMedia's homepage!
Find More Posts by sketchMedia