Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can select * usage ever be justified?

Tags:

sql

select

I've always preached to my developers that SELECT * is evil and should be avoided like the plague.

Are there any cases where it can be justified?

I'm not talking about COUNT(*) - which most optimizers can figure out.

Edit

I'm talking about production code.

And one great example I saw of this bad practice was a legacy asp application that used select * in a stored procedure, and used ADO to loop through the returned records, but got the columns by index. You can imagine what happened when a new field was added somewhere other than the end of the field list.

like image 346
ScottE Avatar asked Sep 03 '10 12:09

ScottE


People also ask

Why select * is not recommended?

When you use select * you're make it impossible to profile, therefore you're not writing clear & straightforward code and you are going against the spirit of the quote. select * is an anti-pattern. So selecting columns is not a premature optimization.

Should you use select * in SQL?

Conclusion. That is why you should not use SELECT * in an SQL query anymore. It's always better to use the explicit column list in a SELECT query than a * wild card. It not only improves performance but also makes your code more explicit.

What is the meaning of * in select statement?

The asterisk (*) in the select list indicates that all columns of the Book table should be included in the result set.

What select * means?

SELECT == It orders the computer to include or select each content from the database name(table ) . (*) == means all {till here code means include all from the database.} FROM == It refers from where we have to select the data.


2 Answers

I'm quite happy using * in audit triggers.

In that case it can actually prove a benefit because it will ensure that if additional columns are added to the base table it will raise an error so it cannot be forgotten to deal with this in the audit trigger and/or audit table structure.

(Like dotjoe) I am also happy using it in derived tables and column table expressions. Though I habitually do it the other way round.

WITH t      AS (SELECT *,                 ROW_NUMBER() OVER (ORDER BY a) AS RN          FROM   foo) SELECT a,        b,        c,        RN FROM   t;  

I'm mostly familiar with SQL Server and there at least the optimiser has no problem recognising that only columns a,b,c will be required and the use of * in the inner table expression does not cause any unnecessary overhead retrieving and discarding unneeded columns.

In principle SELECT * ought to be fine in a view as well as it is the final SELECT from the view where it ought to be avoided however in SQL Server this can cause problems as it stores column metadata for views which is not automatically updated when the underlying tables change and the use of * can lead to confusing and incorrect results unless sp_refreshview is run to update this metadata.

like image 180
Martin Smith Avatar answered Oct 26 '22 14:10

Martin Smith


There are many scenarios where SELECT * is the optimal solution. Running ad-hoc queries in Management Studio just to get a sense of the data you're working with. Querying tables where you don't know the column names yet because it's the first time you've worked with a new schema. Building disposable quick'n'dirty tools to do a one-time migration or data export.

I'd agree that in "proper" development, you should avoid it - but there's lots of scenarios where "proper" development isn't necessarily the optimum solution to a business problem. Rules and best practices are great, as long as you know when to break them. :)

like image 28
Dylan Beattie Avatar answered Oct 26 '22 13:10

Dylan Beattie