哪个更快/最好?SELECT * 或 SELECT column1、colum2、column3 等 [英] Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc

查看:18
本文介绍了哪个更快/最好?SELECT * 或 SELECT column1、colum2、column3 等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在编写 SQL 命令时使用 SELECT * 通常是不好的做法,因为它对于您特别需要的 SELECT 列更有效.

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.

如果我需要 SELECT 表中的每一列,我应该使用

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

SELECT * FROM TABLE

SELECT column1, colum2, column3, etc. FROM TABLE

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

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.

更新:我可能应该指定我真正想要做一个SELECT *的唯一情况是当我选择来自一张我知道所有列的表中的数据总是需要检索,即使添加了新列.

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.

推荐答案

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

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.

这篇关于哪个更快/最好?SELECT * 或 SELECT column1、colum2、column3 等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆