1script - 4:51 pm on Aug 27, 2010 (gmt 0)
well its ALLWAYS good practice to only select the fields you want to process, and this will reduce the memory usage of the database.
I wish it was ALWAYS this easy! The actual answer would greatly depend on the DB and what exactly you do with the data after you've selected it. For example, in MySQL (at least in the MyISAM DB) you have to be really careful about which fields you drop from selecting because if you accidentally removed a field that you don't need for further data processing but use for sorting, you'll force mysql to open the same table twice: first open a temporary table and fill it with the actual fields you said you wanted, then open THE ENTIRE table again, select the omitted field and sort the first table based on results of the second select. This can throw a monkey wrench into your memory calculations when you set your server up: you may easily run out of available memory for those temporary tables storage and start saving stuff to disk which is REALLY slow.
This is what I'm talking about:
SELECT * FROM `posts` ORDER BY `date` DESC will execute faster (or much faster if you throw a WHERE condition in there) than
SELECT `subject`,`body`,`author` FROM `posts` ORDER BY `date` DESC because in the latter example I forgot to select `date` on the first pass and mysql will have to do the second. Even though I did not need the `date` for later processing, I have to not forget to include it into the SELECT if I do something with it in the same SQL statement.
So, yeah, as a general rule "Select only what you need" is true but you have to have an understanding of what exactly is it that you need - sometimes you need more than what's obvious.