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

后端 未结 30 3073
清歌不尽
清歌不尽 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条回答
  • You should really be selecting only the fields you need, and only the required number, i.e.

    SELECT Field1, Field2 FROM SomeTable WHERE --(constraints)
    

    Outside of the database, dynamic queries run the risk of injection attacks and malformed data. Typically you get round this using stored procedures or parameterised queries. Also (although not really that much of a problem) the server has to generate an execution plan each time a dynamic query is executed.

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

    You should only select the columns that you need. Even if you need all columns it's still better to list column names so that the sql server does not have to query system table for columns.

    Also, your application might break if someone adds columns to the table. Your program will get columns it didn't expect too and it might not know how to process them.

    Apart from this if the table has a binary column then the query will be much more slower and use more network resources.

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

    If you need every column then just use SELECT * but remember that the order could potentially change so when you are consuming the results access them by name and not by index.

    I would ignore comments about how * needs to go get the list - chances are parsing and validating named columns is equal to the processing time if not more. Don't prematurely optimize ;-)

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

    SELECT * is a bad practice even if the query is not sent over a network.

    1. Selecting more data than you need makes the query less efficient - the server has to read and transfer extra data, so it takes time and creates unnecessary load on the system (not only the network, as others mentioned, but also disk, CPU etc.). Additionally, the server is unable to optimize the query as well as it might (for example, use covering index for the query).
    2. After some time your table structure might change, so SELECT * will return a different set of columns. So, your application might get a dataset of unexpected structure and break somewhere downstream. Explicitly stating the columns guarantees that you either get a dataset of known structure, or get a clear error on the database level (like 'column not found').

    Of course, all this doesn't matter much for a small and simple system.

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

    SELECT * is necessary if one wants to obtain metadata such as the number of columns.

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

    While explicitly listing columns is good for performance, don't get crazy.

    So if you use all the data, try SELECT * for simplicity (imagine having many columns and doing a JOIN... query may get awful). Then - measure. Compare with query with column names listed explicitly.

    Don't speculate about performance, measure it!

    Explicit listing helps most when you have some column containing big data (like body of a post or article), and don't need it in given query. Then by not returning it in your answer DB server can save time, bandwidth, and disk throughput. Your query result will also be smaller, which is good for any query cache.

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