SELECT * vs SELECT k1, k2...

  • Without thinking I qualify the column names of a table when using SELECT instead of using SELECT *, because this is best practice/more efficient, I have learned. But I have never wondered why this is so.

    Can any1 explain this for me?

    Greetz,
    Hans Brouwer

  • one of the reason is

    optimizer will have to search the column names and its data type from the system table. It will take some extra overheard.

    karthik

  • If you only select the columns you want, you will be returning less data (obviously).

    Also, SQL Server may be able to get the columns you want from an index, without having to go to the actual data in the table, so you will get a much more efficient and performant query plan. If you do a SELECT *, it will always have to go to the table, and you are more likley to get table scans than efficient index seeks.

  • apart from performance benefits, it is likely that if someone else adds an additional column, there will be problems in the application because it returns more columns than desired.

  • Rajan John (9/24/2008)


    apart from performance benefits, it is likely that if someone else adds an additional column, there will be problems in the application because it returns more columns than desired.

    Or vice versa, a column removed may cause issue

    Also, it's likely table A will have ID field that joins to table B which also has ID field

    SELECT * will return 2 ID columns

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005
  • Tnx for answering all. Glad to read there are several reasons(and good ones) for using qualified columns.

    Greetz,
    Hans Brouwer

  • It also makes your code easier to read.

  • Except in rare cases like a pass-through view (like a synonym), SELECT * will frequently (usually) make it impossible to do index seeks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply