SELECT * versus SELECT [column names]

  • One of the big optimization tips when working with an Access database backend is to explicity state the field names to be returned in a query, as opposed to using the asterick.

    Is this also true when working with SQLServer? Is it more efficient to use the astrick or to state the column names?

    I'm asking the question withing the context of working with views where the columns are limited to begin with.

  • The real tuning is to NOT returned columns you don't need.

    Select * or select 100 column names won't change much on sql server. If there's a difference it's measured in nanoseconds.

  • Right because for ever column you return that you don't need SQLServer is doing unneccessary work.

  • david.holley (5/11/2011)


    Right because for ever column you return that you don't need SQLServer is doing unneccessary work.

    And potentially ignoring indexes it could otherwise have used.

    The other reason for not using select * is future-proofing. If, in a month time, someone adds a varbinary(max) column containing an image to the table (or view), and you're using select *, suddenly your app is getting massive amounts of data is doesn't need.

    With a view, someone alters the view and changes column order. With select * suddenly you're getting columns in a different order to what you expected. Maybe bad, maybe not, depends on how the app is coded. Use column names and it's not even a concern.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • The habit of using column (field) names is very ingrained into me. Using * honestly feels bad at this point. I was just curious not being entirely familary with SQLServer optimization.

    I'm developing an ASP.NET app so its a SELECT statements on top of views for now. I'll probably migrate to using views specific to the asp pages as in vw_CustomerRecords_OpenOrders.ASPX to tie the view to the page that it serves.

  • david.holley (5/11/2011)


    The habit of using column (field) names is very ingrained into me. Using * honestly feels bad at this point. I was just curious not being entirely familary with SQLServer optimization.

    I'm developing an ASP.NET app so its a SELECT statements on top of views for now. I'll probably migrate to using views specific to the asp pages as in vw_CustomerRecords_OpenOrders.ASPX to tie the view to the page that it serves.

    Like Gail said, the real problem is the query execution itself. The server will always have to do a table scan (well, too often anyways). And you won't have any chance to do further index optimization.

    Figuring out the 10 columns instead of * is an insignificant hit.... if there's one.

  • Is there any significant cost to having two views with more or less the same columns with just a few differences between the two?

  • No. views are just saved select statements.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • The size of the code of the view in bytes in the system table of the db.

    So NO :-).

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

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