What is the reason not to use select *?

后端 未结 20 3258
独厮守ぢ
独厮守ぢ 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 08:00

    1. In a roundabout way you are breaking the modularity rule about using strict typing wherever possible. Explicit is almost universally better.

    2. Even if you now need every column in the table, more could be added later which will be pulled down every time you run the query and could hurt performance. It hurts performance because

      • You are pulling more data over the wire; and
      • Because you might defeat the optimizer's ability to pull the data right out of the index (for queries on columns that are all part of an index.) rather than doing a lookup in the table itself

    When TO use select *

    When you explicitly NEED every column in the table, as opposed to needing every column in the table THAT EXISTED AT THE TIME YOU WROTE THE QUERY. For example, if were writing an DB management app that needed to display the entire contents of the table (whatever they happened to be) you might use that approach.

提交回复
热议问题