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

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

    It is NOT faster to use explicit field names versus *, if and only if, you need to get the data for all fields.

    Your client software shouldn't depend on the order of the fields returned, so that's a nonsense too.

    And it's possible (though unlikely) that you need to get all fields using * because you don't yet know what fields exist (think very dynamic database structure).

    Another disadvantage of using explicit field names is that if there are many of them and they're long then it makes reading the code and/or the query log more difficult.

    So the rule should be: if you need all the fields, use *, if you need only a subset, name them explicitly.

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

    There are four big reasons that select * is a bad thing:

    1. The most significant practical reason is that it forces the user to magically know the order in which columns will be returned. It's better to be explicit, which also protects you against the table changing, which segues nicely into...

    2. If a column name you're using changes, it's better to catch it early (at the point of the SQL call) rather than when you're trying to use the column that no longer exists (or has had its name changed, etc.)

    3. Listing the column names makes your code far more self-documented, and so probably more readable.

    4. If you're transferring over a network (or even if you aren't), columns you don't need are just waste.

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

    Performance wise I have seen comments that both are equal. but usability aspect there are some +'s and -'s

    When you use a (select *) in a query and if some one alter the table and add new fields which do not need for the previous query it is an unnecessary overhead. And what if the newly added field is a blob or an image field??? your query response time is going to be really slow then.

    In other hand if you use a (select col1,col2,..) and if the table get altered and added new fields and if those fields are needed in the result set, you always need to edit your select query after table alteration.

    But I suggest always to use select col1,col2,... in your queries and alter the query if the table get altered later...

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

    Given your specification that you are selecting all columns, there is little difference at this time. Realize, however, that database schemas do change. If you use SELECT * you are going to get any new columns added to the table, even though in all likelihood, your code is not prepared to use or present that new data. This means that you are exposing your system to unexpected performance and functionality changes.

    You may be willing to dismiss this as a minor cost, but realize that columns that you don't need still must be:

    1. Read from database
    2. Sent across the network
    3. Marshalled into your process
    4. (for ADO-type technologies) Saved in a data-table in-memory
    5. Ignored and discarded / garbage-collected

    Item #1 has many hidden costs including eliminating some potential covering index, causing data-page loads (and server cache thrashing), incurring row / page / table locks that might be otherwise avoided.

    Balance this against the potential savings of specifying the columns versus an * and the only potential savings are:

    1. Programmer doesn't need to revisit the SQL to add columns
    2. The network-transport of the SQL is smaller / faster
    3. SQL Server query parse / validation time
    4. SQL Server query plan cache

    For item 1, the reality is that you're going to add / change code to use any new column you might add anyway, so it is a wash.

    For item 2, the difference is rarely enough to push you into a different packet-size or number of network packets. If you get to the point where SQL statement transmission time is the predominant issue, you probably need to reduce the rate of statements first.

    For item 3, there is NO savings as the expansion of the * has to happen anyway, which means consulting the table(s) schema anyway. Realistically, listing the columns will incur the same cost because they have to be validated against the schema. In other words this is a complete wash.

    For item 4, when you specify specific columns, your query plan cache could get larger but only if you are dealing with different sets of columns (which is not what you've specified). In this case, you do want different cache entries because you want different plans as needed.

    So, this all comes down, because of the way you specified the question, to the issue resiliency in the face of eventual schema modifications. If you're burning this schema into ROM (it happens), then an * is perfectly acceptable.

    However, my general guideline is that you should only select the columns you need, which means that sometimes it will look like you are asking for all of them, but DBAs and schema evolution mean that some new columns might appear that could greatly affect the query.

    My advice is that you should ALWAYS SELECT specific columns. Remember that you get good at what you do over and over, so just get in the habit of doing it right.

    If you are wondering why a schema might change without code changing, think in terms of audit logging, effective/expiration dates and other similar things that get added by DBAs for systemically for compliance issues. Another source of underhanded changes is denormalizations for performance elsewhere in the system or user-defined fields.

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

    Performance wise, SELECT with specific columns can be faster (no need to read in all the data). If your query really does use ALL the columns, SELECT with explicit parameters is still preferred. Any speed difference will be basically unnoticeable and near constant-time. One day your schema will change, and this is good insurance to prevent problems due to this.

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

    Select is equally efficient (in terms of velocity) if you use * or columns.

    The difference is about memory, not velocity. When you select several columns SQL Server must allocate memory space to serve you the query, including all data for all the columns that you've requested, even if you're only using one of them.

    What does matter in terms of performance is the excecution plan which in turn depends heavily on your WHERE clause and the number of JOIN, OUTER JOIN, etc ...

    For your question just use SELECT *. If you need all the columns there's no performance difference.

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