What is the reason not to use select *?

后端 未结 20 2776
独厮守ぢ
独厮守ぢ 2020-11-21 07:14

I\'ve seen a number of people claim that you should specifically name each column you want in your select query.

Assuming I\'m going to use all of the columns anyway

相关标签:
20条回答
  • 2020-11-21 07:48

    I don't use SELECT * simply because it is nice to see and know what fields I am retrieving.

    0 讨论(0)
  • 2020-11-21 07:50

    Just to add one thing that no one else has mentioned. Select * returns all the columns, someone may add a column later that you don't necessarily want the users to be able to see such as who last updated the data or a timestamp or notes that only managers should see not all users, etc.

    Further, when adding a column, the impact on existing code should be reviewed and considered to see if changes are needed based on what information is stored in the column. By using select *, that review will often be skipped because the developer will assume that nothing will break. And in fact nothing may explicitly appear to break but queries may now start returning the wrong thing. Just because nothing explicitly breaks, doesn't mean that there should not have been changes to the queries.

    0 讨论(0)
  • 2020-11-21 07:51

    If your code depends on the columns being in a specific order, your code will break when there are changes to the table. Also, you may be fetching too much from the table when you select *, especially if there is a binary field in the table.

    Just because you are using all the columns now, it doesn't mean someone else isn't going to add an extra column to the table.

    It also adds overhead to the plan execution caching since it has to fetch the meta data about the table to know what columns are in *.

    0 讨论(0)
  • 2020-11-21 07:51

    If your application gets data with SELECT * and the table structure in the database is changed (say a column is removed), your application will fail in every place that you reference the missing field. If you instead include all the columns in your query, you application will break in the (hopefully) one place where you initially get the data, making the fix easier.

    That being said, there are a number of situations in which SELECT * is desirable. One is a situation that I encounter all the time, where I need to replicate an entire table into another database (like SQL Server to DB2, for example). Another is an application written to display tables generically (i.e. without any knowledge of any particular table).

    0 讨论(0)
  • 2020-11-21 07:51

    Selecting only the columns you need keeps the dataset in memory smaller and therefor keeps your application faster.

    Also, a lot of tools (e.g. stored procedures) cache query execution plans too. If you later add or remove a column (particularly easy if you're selecting off a view), the tool will often error when it doesn't get back results that it expects.

    0 讨论(0)
  • 2020-11-21 07:53

    It's ok when you're doing exists(select * ...) since it never gets expanded. Otherwise it's really only useful when exploring tables with temporary select statments or if you had a CTE defined above and you want every column without typing them all out again.

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