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

后端 未结 30 3072
清歌不尽
清歌不尽 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:37

    Absolutely define the columns you want to SELECT every time. There is no reason not to and the performance improvement is well worth it.

    They should never have given the option to "SELECT *"

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

    As with most problems, it depends on what you want to achieve. If you want to create a db grid that will allow all columns in any table, then "Select *" is the answer. However, if you will only need certain columns and adding or deleting columns from the query is done infrequently, then specify them individually.

    It also depends on the amount of data you want to transfer from the server. If one of the columns is a defined as memo, graphic, blob, etc. and you don't need that column, you'd better not use "Select *" or you'll get a whole bunch of data you don't want and your performance could suffer.

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

    To add on to what everyone else has said, if all of your columns that you are selecting are included in an index, your result set will be pulled from the index instead of looking up additional data from SQL.

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

    Gonna get slammed for this, but I do a select * because almost all my data is retrived from SQL Server Views that precombine needed values from multiple tables into a single easy to access View.

    I do then want all the columns from the view which won't change when new fields are added to underlying tables. This has the added benefit of allowing me to change where data comes from. FieldA in the View may at one time be calculated and then I may change it to be static. Either way the View supplies FieldA to me.

    The beauty of this is that it allows my data layer to get datasets. It then passes them to my BL which can then create objects from them. My main app only knows and interacts with the objects. I even allow my objects to self-create when passed a datarow.

    Of course, I'm the only developer, so that helps too :)

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

    The result is too huge. It is slow to generate and send the result from the SQL engine to the client.

    The client side, being a generic programming environment, is not and should not be designed to filter and process the results (e.g. the WHERE clause, ORDER clause), as the number of rows can be huge (e.g. tens of millions of rows).

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

    It depends on the version of your DB server, but modern versions of SQL can cache the plan either way. I'd say go with whatever is most maintainable with your data access code.

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