I remember I read about this once. Is it really faster to list all desired columns instead of simply using *
even if all columns are needed?
Thanks, Martti Laine
I remember I read about this once. Is it really faster to list all desired columns instead of simply using *
even if all columns are needed?
Thanks, Martti Laine
I have never, never seen the debate about select column-list vs. select * be about performance of the query when the results are exactly the same.
The question should always be, when deciding between the forms, which is more maintainable, and the answer depends on turn on the query's use.
It depends. If you are selecting only 3 columns while your tables has 100 columns. Than it's faster.
If you select all columns anyway, than it's simply a different notation.
As long as you use all the fields in the table, then there is no significant performance difference between using *
and listing all the fields.
The difference comes when you change the table layout in some way. If you add another field, the query using *
will automatically start returning that field also, eventhough it's not going to be used. This will increase the overhead, and potentially make the code fail when the data in a record becomes larger than the buffer used to fetch records.
Also listing the fields means that they are returned in the order specified. If you change the order of the fields in the table, the query using *
will also change the order in which the fields are returned, which may cause problems in your code. For example, blob fields (e.g. text) have to be retrieved in the order that they are returned, or the values are discarded.
One thing that makes it possibly a lot faster is that when you give the fields, you also give the order. Otherwise I need to retreive the field either by string, or first get he orginal. I simply dont know whether "fieldX" is the first or 10th field.
This can result in either more complicated code, or a lot slowdown (if one has the string lookup within the loop).
The real performance difference comes when you the columns that you select are all available through an index. In that case MySQL (and several other DBMS) will do an "index only select" which might a lot faster than doing an index-based select from the table itself.
Additionally the overhead in sending unneeded data over the wire will of course slow down retrieval as well.
And finally is simply good coding practice to only select the data you need.