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

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

    It's always better to specify the columns you need, if you think about it one time, SQL doesn't have to think "wtf is *" every time you query. On top of that, someone later may add columns to the table that you actually do not need in your query and you'll be better off in that case by specifying all of your columns.

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

    definitely defining the columns, because SQL Server will not have to do a lookup on the columns to pull them. If you define the columns, then SQL can skip that step.

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

    Specifying column names is definitely faster - for the server. But if

    1. performance is not a big issue (for example, this is a website content database with hundreds, maybe thousands - but not millions - of rows in each table); AND
    2. your job is to create many small, similar applications (e.g. public-facing content-managed websites) using a common framework, rather than creating a complex one-off application; AND
    3. flexibility is important (lots of customization of the db schema for each site);

    then you're better off sticking with SELECT *. In our framework, heavy use of SELECT * allows us to introduce a new website managed content field to a table, giving it all of the benefits of the CMS (versioning, workflow/approvals, etc.), while only touching the code at a couple of points, instead of a couple dozen points.

    I know the DB gurus are going to hate me for this - go ahead, vote me down - but in my world, developer time is scarce and CPU cycles are abundant, so I adjust accordingly what I conserve and what I waste.

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

    One reason that selecting specific columns is better is that it raises the probability that SQL Server can access the data from indexes rather than querying the table data.

    Here's a post I wrote about it: The real reason select queries are bad index coverage

    It's also less fragile to change, since any code that consumes the data will be getting the same data structure regardless of changes you make to the table schema in the future.

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

    Naming each column you expect to get in your application also ensures your application won't break if someone alters the table, as long as your columns are still present (in any order).

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

    What everyone above said, plus:

    If you're striving for readable maintainable code, doing something like:

    SELECT foo, bar FROM widgets;

    is instantly readable and shows intent. If you make that call you know what you're getting back. If widgets only has foo and bar columns, then selecting * means you still have to think about what you're getting back, confirm the order is mapped correctly, etc. However, if widgets has more columns but you're only interested in foo and bar, then your code gets messy when you query for a wildcard and then only use some of what's returned.

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