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.

我听说在编写SQL命令时使用SELECT *通常是不好的做法,因为选择特定需要的列更有效。

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.

在这种情况下效率真的重要吗?如果您确实需要所有的数据,那么我认为SELECT *在内部会更理想,但是我说的是对数据库没有真正的理解。

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.

更新:我可能应该指定,我真正想要执行SELECT *的情况是,当我从一个表中选择数据时,我知道所有列都需要检索,即使添加了新列。

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.

然而,考虑到我所见过的回复,这似乎仍然是一个糟糕的想法,而且选择*不应该被用于更多我曾经考虑过的技术原因。

46 个解决方案

#1


143

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.

选择特定列更好的一个原因是,它提高了SQL Server从索引访问数据而不是查询表数据的可能性。

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.

更改也不那么脆弱,因为使用数据的任何代码都将获得相同的数据结构,而不管将来对表模式做什么更改。

更多相关文章

  1. Ms sql 2005 中的bit 数据类型
  2. 从Visual Studio 2012访问Netezza SQL数据库时出错
  3. 数据库 T-SQL 里的 GO 是什么意思
  4. 删除重复数据,只保留ID最小的一条数据
  5. mysql中将多条记录合并成一行数据进行显示
  6. SQLServer---查询过程中的数据类型转化
  7. 要在SQL数据库中根据身份证号码查询出性别(有15位的,也有18位的),怎
  8. asp.net按路径把图片上传到sql数据库
  9. linux下使用c++语言对mysql数据库操作

随机推荐

  1. 无法连接远程MySQL数据库的解决方案
  2. 使用mysql和PHP从本地主机连接到远程数据
  3. 在MySQL解释计划中“选择优化的表”的意
  4. 我是初学者,使用mysql时候总是出现comman
  5. SQL内连接不显示数据
  6. 如何从标准化表中获取最流行的标签?
  7. Mysql中FIND_IN_SET()和IN区别简析
  8. 在H2数据库中插入时间——函数“PARSEDAT
  9. beego_mysql,redis,mongo连接(20170307)
  10. 如何在MySQL数据库和JPA中使用Spring Boo