4

I was wondering which is best practice. Lest say I have a table with 10+ columns and I want to select data from it.

I've heard that 'select *' is better since selecting specific columns makes the database search for these columns before selecting while selecting all just grabs everything. On the other hand, what if the table has a lot of columns in it?

Is that true?

Thanks

developer82
  • 13,237
  • 21
  • 88
  • 153

6 Answers6

5

It is best practice to explicitly name the columns you want to select.

As Mitch just said the performance isn't different. I even heard that looking up the actual columns names when using * is slower.

But the advantage is that when your table changes then your select does not change when you name your columns.

Community
  • 1
  • 1
juergen d
  • 201,996
  • 37
  • 293
  • 362
4

I think these two questions here and here have satisfactory answers. * is not better, actually it is slower is one reason that select * is not good. In addition to this, according to OMG Ponies, select * is anti-pattern. See the questions in the links for detail.

Community
  • 1
  • 1
Barış Akkurt
  • 2,255
  • 3
  • 22
  • 37
2

selecting specific columns is better as it is raises the probability that SQL Server can access the data from indexes rather than querying the table data.

It's also require less changes, since any code that consumes the data will be getting the same data structure regardless of changes you make to the table schema in the future.

SoftSan
  • 2,482
  • 3
  • 23
  • 54
1

Definetly not. Try making a SELECT * from a table which has millions of rows and tens of columns.

The performance with SELECT * will be worse.

Semih Yagcioglu
  • 4,011
  • 1
  • 26
  • 43
0

It depends on what you're about to do with the result. Selecting unnecessary data is not a good practice either. You wouldn't create a bunch of variables with values you would never use. So selecting many columns you don't need is not a good idea either.

MaGnetas
  • 4,918
  • 4
  • 32
  • 52
0

It depends.

Selecting all columns can make query slower because of need of reading all columns from disk -- if there are a lot of string columns (which are not in index) then it can have huge impact on query (IO) performance. And from my practise -- you rely need all columns.

From the other hand -- for small database with a few user and good enough hardware it's much easier to select just all columns -- especially if schema changes often.

However -- I would always recommended to explicitly select columns to make sure it doesn't hurt performance.

Grzegorz Gierlik
  • 11,112
  • 4
  • 47
  • 55