Which is faster/best? SELECT * or SELECT column1,

2018-12-31 03:30发布

I've heard that SELECT * is generally bad practice to use when writing SQL commands because it is more efficient to SELECT columns you specifically need.

If I need to SELECT every column in a table, should I use

SELECT * FROM TABLE

or

SELECT column1, colum2, column3, etc. FROM TABLE

Does the efficiency really matter in this case? I'd think SELECT * would be more optimal internally if you really need all of the data, but I'm saying this with no real understanding of database.

I'm curious to know what the best practice is in this case.

UPDATE: I probably should specify that the only situation where I would really want to do a SELECT * is when I'm selecting data from one table where I know all columns will always need to be retrieved, even when new columns are added.

Given the responses I've seen however, this still seems like a bad idea and SELECT * should never be used for a lot more technical reasons that I ever though about.

标签: sql database
30条回答
泪湿衣
2楼-- · 2018-12-31 03:47

In terms of execution efficiency I am not aware of any significant difference. But for programmers efficiency I would write the names of the fields because

  • You know the order if you need to index by number, or if your driver behaves funny on blob-values, and you need a definite order
  • You only read the fields you need, if you should ever add more fields
  • You get an sql-error if you misspell or rename a field, not an empty value from a recordset/row
  • You can better read what's going on.
查看更多
几人难应
3楼-- · 2018-12-31 03:49

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).

查看更多
余生请多指教
4楼-- · 2018-12-31 03:50

As with most problems, it depends on what you want to achieve. If you want to create a db grid that will allow all columns in any table, then "Select *" is the answer. However, if you will only need certain columns and adding or deleting columns from the query is done infrequently, then specify them individually.

It also depends on the amount of data you want to transfer from the server. If one of the columns is a defined as memo, graphic, blob, etc. and you don't need that column, you'd better not use "Select *" or you'll get a whole bunch of data you don't want and your performance could suffer.

查看更多
梦该遗忘
5楼-- · 2018-12-31 03:50

SELECT * is necessary if one wants to obtain metadata such as the number of columns.

查看更多
高级女魔头
6楼-- · 2018-12-31 03:52

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.

查看更多
残风、尘缘若梦
7楼-- · 2018-12-31 03:54

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.

查看更多
登录 后发表回答