SQL: Using Select *

Possible duplicate:
Which is faster / better? SELECT * or SELECT column1, colum2, column3 etc.

Is it wrong to use Select * ?

I went through the old code and saw some SELECT * statements. My previous colleague told me that Select * was bad practice, but I really could not understand the reason (unless, of course, I only needed to return a few fields). But for the full "details of receipt" (queries of type "Get by type"), the choice * seems to be correct.

+5
sql
source share
7 answers

This is bad practice.

If your pattern changes along the way, the calling application may receive more fields than it knows what to do with them.

In addition, you get more information than you need, which affects performance.

In addition, this means that you do not know what columns are.

+5
source share

Using SELECT * is bad practice for two reasons:

  • It can return additional columns that you do not need, losing bandwidth.
  • It can break your code if someone adds a column
+4
source share

Yes, select * - this is bad practice. First, other developers do not understand which columns you are actually using. Do you actually use them all? What happens when you add columns, do you use them too? This makes it difficult to reorganize column columns if necessary. Secondly, there are some cases where some database systems will remember which columns existed at the time the object was created. For example, if you create a stored procedure using Select *, it will bake in the columns that exist in the table at compile time. If the table changes, it does not reflect these changes in the stored procedure. There really is no reason to use Select * outside of laziness.

+2
source share

Yes, this is considered bad practice.

It is better to specify an explicit list of columns, especially if the table contains many columns, and you really need some of them.

+1
source share

If any schema changes occur (additional columns are added), they will be captured by your application. This might be undesirable, say, if you bind the grid dynamically to a DataTable . In addition, this is associated with high costs for network communications.

Even if you select all the columns for today, identify the columns by name - its readable and explicit. Any additional columns will not cause any problems with your code.

+1
source share

When you use SELECT * , you choose to immediately sell the product (faster than writing a request) to increase service performance (if your basic request changes and thus breaks the dependent code / requests). The “bad” practice is risk management.

+1
source share

Even if you need to select all columns, it is better to specify them and then use "select *"

0
source share

All Articles