Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc

后端 未结 30 3038
清歌不尽
清歌不尽 2020-11-21 23:59

I\'ve heard that SELECT * is generally bad practice to use when writing SQL commands because it is more efficient to SELECT columns you specificall

相关标签:
30条回答
  • 2020-11-22 00:43

    One reason it's better practice to spell out exactly which columns you want is because of possible future changes in the table structure.

    If you are reading in data manually using an index based approach to populate a data structure with the results of your query, then in the future when you add/remove a column you will have headaches trying to figure out what went wrong.

    As to what is faster, I'll defer to others for their expertise.

    0 讨论(0)
  • 2020-11-22 00:44

    Lots of good reasons answered here so far, here's another one that hasn't been mentioned.

    Explicitly naming the columns will help you with maintenance down the road. At some point you're going to be making changes or troubleshooting, and find yourself asking "where the heck is that column used".

    If you've got the names listed explicitly, then finding every reference to that column -- through all your stored procedures, views, etc -- is simple. Just dump a CREATE script for your DB schema, and text search through it.

    0 讨论(0)
  • 2020-11-22 00:44

    And remember if you have an inner join by definition you do not need all the columns as the data in the join columns is repeated.

    It's not like listing columns in SQl server is hard or even time-consuming. You just drag them over from the object browser (you can get all in one go by dragging from the word columns). To put a permanent performance hit on your system (becasue this can reduce the use of indexes and becasue sending unneeded data over the network is costly) and make it more likely that you will have unexpected problems as the database changes (sometimes columns get added that you do not want the user to see for instance) just to save less than a minute of development time is short-sighted and unprofessional.

    0 讨论(0)
  • 2020-11-22 00:44

    In terms of execution efficiency I am not aware of any significant difference. But for programmers efficiency I would write the names of the fields because

    • You know the order if you need to index by number, or if your driver behaves funny on blob-values, and you need a definite order
    • You only read the fields you need, if you should ever add more fields
    • You get an sql-error if you misspell or rename a field, not an empty value from a recordset/row
    • You can better read what's going on.
    0 讨论(0)
  • 2020-11-22 00:46

    The problem with "select *" is the possibility of bringing data you don't really need. During the actual database query, the selected columns don't really add to the computation. What's really "heavy" is the data transport back to your client, and any column that you don't really need is just wasting network bandwidth and adding to the time you're waiting for you query to return.

    Even if you do use all the columns brought from a "select *...", that's just for now. If in the future you change the table/view layout and add more columns, you'll start bring those in your selects even if you don't need them.

    Another point in which a "select *" statement is bad is on view creation. If you create a view using "select *" and later add columns to your table, the view definition and the data returned won't match, and you'll need to recompile your views in order for them to work again.

    I know that writing a "select *" is tempting, 'cause I really don't like to manually specify all the fields on my queries, but when your system start to evolve, you'll see that it's worth to spend this extra time/effort in specifying the fields rather than spending much more time and effort removing bugs on your views or optimizing your app.

    0 讨论(0)
  • 2020-11-22 00:47

    Specifying the column list is usually the best option because your application won't be affected if someone adds/inserts a column to the table.

    0 讨论(0)
提交回复
热议问题