views:

136

answers:

8

I'm a self taught developer and ive always been told not to use SELECT *, but most of my queries require to know all the values of a certain row...

what should i use then? should i list ALL of the properties every time? like Select elem1,elem2,elem3,....,elem15 FROM...?

thanks

+1  A: 

List only the columns that you need, ideally with a table alias:

SELECT t.elem1,
       t.elem2
  FROM YOUR_TABLE t

The presence of a table alias helps demonstrate what is a column (and where it's from) vs a derived column.

OMG Ponies
+3  A: 

If you really need all the columns and you're fetching the results by name, I would go ahead and use SELECT *. If you're fetching the row results by index, then it makes sense to specify the column names or else they might not be in the order you expect (especially if the table schema changes).

Nick
@Nick, I have NEVER had a need to fetch columns by index rather than name. I can't even come up with a valid scenario where you would.
Chad
Chad, I am glad. I certainly wouldn't choose index access over name access; I just know that there exist APIs that select results by index, and the OP hasn't specified what language or platform he is using to talk to MySQL.
Nick
I don't know why you would either, but PHP's [`mysql_fetch_row`](http://ca.php.net/manual/en/function.mysql-fetch-row.php) will return a numerically-indexed array, for an example of this occurring in the wild.
Daniel Vandersluis
It's scary the number of apps I've seen where people are using the index instead of column names. +1 in the event that just ONE person reads this and thinks "hmm, maybe that's not a good idea"
Chris Lively
@Chad: The numeric index for column reference is called an ordinal, and you're correct--it's available, but I've [yet to see a good reason to use it](http://stackoverflow.com/questions/2253040/benefits-of-using-sql-ordinal-position-notation).
OMG Ponies
A: 

The only real performance hit you take from using select * is in the bandwidth required to send back extra columns in your result set, if they're not necessary. Other than that, there's nothing inherently "bad" about using select *.

Matt
Not true. http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select
Chris Lively
I have been pwned.
Matt
+2  A: 

If you absolutely have to use *, try to limit it to a specific table; e.g.:

SELECT t.*
  FROM mytable t
Adam Bernier
Good point, I wouldn't use `*` on its own when joining tables.
Nick
+3  A: 

SELECT * FROM ... is not always the best way to go unless you need all columns. That's because if for example a table has 10 columns and you only need 2-3 of them and these columns are indexed, then if you use SELECT * the query will run slower, because the server must fetch all rows from the datafiles. If instead you used only the 2-3 columns that you actually needed, then the server could run the query much faster if the rows were fetch from a covering index. A covering index is one that is used to return results without reading the datafile.

So, use SELECT * only when you actually need all columns.

Vasileios Lourdas
+1  A: 

If you are positive that you will always need all the columns then select * should be ok. But my reasoning for avoiding it is: say another developer has another column added to the table which isn't required by your query..then there is overhead. This can get worse as more columns get added.

aeq
A: 

You might SELECT * from a subquery.

Hammerite
A: 

Yes, Select * is bad. You do not state what language you will be using process the returned data. Suppose you receive these records back as an array (not a hash map). In this case, what is in Row[12]? Maybe it was ZipCode when you wrote the app, but guess what happens when someone inserts a field SuiteNumber before ZipCode.

Or suppose the next coder appends a huge blob field to each record. Ouch!

Or a little more subtle: let's assume you're doing a join or subselect, and you have no Text or Blob type fields. MySQL will create any temp files it needs in memory. But as soon as you include a Text field (even TinyText), MySQL will need to create the temp file on disk, and call sort-merge. This won't break the program, but it can kill performance.

Select * is sacrificing maintainability in order to save a little typing.

Guy Gordon