homepage Welcome to WebmasterWorld Guest from 107.20.109.52
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Is SELECT * FROM .slow?
MySQL
migthegreek




msg:4193108
 3:37 pm on Aug 27, 2010 (gmt 0)

I notice in most database abstraction classes for PHP, most select methods simply assume that you wish to select all fields from a row.

I'm having trouble finding any info about the optimisation benefits (if any) of actually specifying which fields to select versus selecting all.

 

Maurice




msg:4193111
 3:46 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.

A lot of mysql/php devs have learned a lot of very bad habits unfortunetly - I was lucky and got taught the ORACLE way and had a DBA one of whose claims to fame was "oh Dijkstra was my first boss"

and dont relay on the ordial position of results - always refer by name.

LifeinAsia




msg:4193112
 3:47 pm on Aug 27, 2010 (gmt 0)

I notice in most database abstraction classes for PHP, most select methods simply assume that you wish to select all fields from a row

For showing examples, it's less confusing to keep things simple. The more details you show, the more likely people will miss the important points.

In practice, it's generally better to just select the fields you need. Especially if you are returning a lot of rows.

Think of it this way, all the fields that you don't need have to be stored into memory. If your application server is on a different box from your database server, that unneeded data also has to be passed across the network.

migthegreek




msg:4193115
 3:49 pm on Aug 27, 2010 (gmt 0)

Thanks.

1script




msg:4193142
 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.

Demaestro




msg:4193145
 4:56 pm on Aug 27, 2010 (gmt 0)

Some other things to consider.

It isn't just the DB that you will affect by returning all fields.

calls to things like mysql_fetch_array will be more costly on the processor as well it also affects the DB_Conn in many cases.

1script is right you do have to make sure that you know which fields you need and it isn't just the ones you are displaying on page.

LifeinAsia




msg:4193161
 5:28 pm on Aug 27, 2010 (gmt 0)

1script- very interesting point! Any idea if other DBs have the same behavior?

1script




msg:4193184
 6:13 pm on Aug 27, 2010 (gmt 0)

@LifeinAsia: if you mean other types of DBs supported by MySQL then the answer is *most likely* yes. I'm just basing my examples on the real optimization work I do these days and the DBs are mostly MyISAM. In any case, I would advise anyone interested to go straight to the source: [dev.mysql.com...] While there, check out other optimization techniques and examples they link to in the right panel.

LifeinAsia




msg:4193187
 6:19 pm on Aug 27, 2010 (gmt 0)

Actually, I technically meant other DBMSes like MS SQL, Oracle, etc.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved