What is the reason not to use select *?

后端 未结 20 3133
独厮守ぢ
独厮守ぢ 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:59

    There are a few reasons:

    1. If the number of columns in a database changes and your application expects there to be a certain number...
    2. If the order of columns in a database changes and your application expects them to be in a certain order...
    3. Memory overhead. 8 unnecessary INTEGER columns would add 32 bytes of wasted memory. That doesn't sound like a lot, but this is for each query and INTEGER is one of the small column types... the extra columns are more likely to be VARCHAR or TEXT columns, which adds up quicker.
    4. Network overhead. Related to memory overhead: if I issue 30,000 queries and have 8 unnecessary INTEGER columns, I've wasted 960kB of bandwidth. VARCHAR and TEXT columns are likely to be considerably larger.

    Note: I chose INTEGER in the above example because they have a fixed size of 4 bytes.

提交回复
热议问题