简体   繁体   English

从表中选择*或从表中选择id,field1,field2,field3 - 最佳实践?

[英]Select * from table OR select id,field1, field2, field3 from table - best practice?

Could someone chime in as to whats a better practice? 有人可以说是一个更好的做法吗? for select queries should I return all or the IDs that I require? 对于选择查询,我应该返回我需要的全部或ID吗?

Efficiency? 效率? Scalability? 可扩展性? etc. 等等

thanks 谢谢

Env: SQL Server 2008, VS2008 (VB) 环境:SQL Server 2008,VS2008(VB)

Always explicitly enumerate your columns. 始终明确枚举您的列。 Never have select * in any production code. 永远不要在任何生产代码中select *

Even situations where it may seemingly make sense can have unintended consequences. 即使看似有意义的情况也会产生意想不到的后果。 For example, you may think to do select * when you have a view that is supposed to mirror the layout of a table, but strange things can happen if you modify the underlying table without regenerating the view. 例如,当您有一个应该镜像表格布局的视图时,您可能会认为select * ,但如果修改基础表而不重新生成视图,则会发生奇怪的事情。

Stay away from select * unless you're typing the query and executing it then and there. 远离select *除非您输入查询并在那里执行它。

Could someone chime in as to whats a better practice? 有人可以说是一个更好的做法吗? for select queries should I return all or the IDs that I require? 对于选择查询,我应该返回我需要的全部或ID吗?

Name your columns. 为列命名。

This is not only a best practice, but can gain more performance. 这不仅是一种最佳实践,而且可以获得更高的性能。

Imagine two queries: 想象一下两个问题:

SELECT  *
FROM    mytable
WHERE   column1 = @somevalue

and

SELECT  id, column1
FROM    mytable
WHERE   column1 = @somevalue

id is a clustered primary key and there is an index on column1 . id是一个聚簇主键, column1上有一个索引。

I'm assuming that your client code processes the variable number of columns correctly, ie the table layout change does not break the code. 我假设您的客户端代码正确处理可变数量的列,即表格布局更改不会破坏代码。 This is a very strong assumption but let's make it. 这是一个非常强大的假设,但让我们做到。

Now, if mytable consists only of id and column1 , the queries are the same. 现在,如果mytable只包含idcolumn1 ,则查询是相同的。

What happens if you add a column2 to mytable ? 如果将column2添加到mytable会发生什么?

The second query (with named columns) still uses the index (since in contains everything the query needs), but the first one needs to select column2 too ( SQL Server does not know you are going to ignore it). 第二个查询(带有命名列)仍然使用索引(因为in包含查询所需的所有内容),但第一个查询也需要选择column2SQL Server不知道你会忽略它)。

This will add a Clustered Table Seek into a plan and your query performance gets worse. 这会将Clustered Table Seek添加到计划中,并且您的查询性能会变差。

Use select col1, col2, col3 from table instead of select * from table1. 使用表中的select col1,col2,col3而不是select * from table1。 This has numerous advantages, as mentioned here and here. 如本文和此处所述,这具有许多优点。

Also see: http://weblogs.sqlteam.com/jeffs/jeffs/archive/2007/07/26/60271.aspx 另见: http//weblogs.sqlteam.com/jeffs/jeffs/archive/2007/07/26/60271.aspx

Is there a difference between Select * and Select [list each col] Select *和Select [list each col]之间是否有区别

Never listen to anyone telling you to always do something in SQL -- alternatively, always be wary of anyone telling you to never do something in SQL :) 永远不要听任何人告诉你总是在SQL中做一些事情 - 或者,总是要警惕任何告诉你永远不要在SQL中做某事的人:)

In the following examples, SELECT * can do no harm and arguably has benefit as regards readability and code maintenance ( DRY and all that): 在以下示例中, SELECT *不会造成任何伤害,并且可以说在可读性和代码维护方面具有优势( DRY和所有这些):


Example 1 例1

When the commalist of attributes has already been specified in an 'inner' scope: 当已经在“内部”范围中指定了属性的commalist时:

SELECT * 
  FROM (
        SELECT col1, col2, col3, col4, col5
          FROM T1 
       ) AS DT1;

Example 2 例2

When using a table value constructor in a CTE and one is compelled (eg in SQL Server!) to wrap the VALUES clause in a table expression ( SELECT..FROM ) eg 在CTE中使用表值构造函数时,一个被强制(例如在SQL Server中!)将VALUES子句包装在表表达式( SELECT..FROM )中,例如

WITH T1
     AS
     (
      SELECT * 
        FROM (
              VALUES (1, 1, 1, 2, 1), 
                     (1, 1, 2, 1, 1), 
                     (1, 2, 1, 1, 1)
             ) AS T (col1, col2, col3, col4, col5)
     )
SELECT ...

OK so this last one is a bit of a strawman but consider that using commalists at every opportunity has caused an error and makes it difficult to debug: 好吧,所以这最后一个是一个稻草人,但考虑到每个机会使用commalists导致错误,并使其难以调试:

WITH T2 (author_name, book_title, ISBN) 
     AS
     (
      SELECT book_title, ISBN, author_name
        FROM (
              VALUES ('9780321189561', 'C. J. Date', 'An Introduction to Database Systems')
             ) AS T (ISBN, author_name, book_title)
     )
SELECT *
  FROM T2;

Always use named columns! 始终使用命名列!

A good example of why it's bad: "select * from table" vs "select colA,colB,etc from table" interesting behaviour in SqlServer2005 一个很好的例子,说明为什么它是坏的: “从表中选择*”与“从表中选择colA,colB等”SqlServer2005中的有趣行为

You should specify the columns in most cases, this works best for future changes and maintenance. 在大多数情况下,您应该指定列,这最适合将来的更改和维护。 It also pulls less data, enhancing performance. 它还可以减少数据量,提高性能。

The reasons to prefer explicitly naming columns over SELECT * FROM our_table are 更喜欢在SELECT * FROM our_table上明确命名列的原因是

  1. Explicitly naming columns in the project expresses our intent more clearly and so contributes to self-documenting code. 在项目中明确命名列可以更清楚地表达我们的意图,从而有助于自我记录代码。
  2. In the future somebody will add one or more columns to the table. 将来有人会在表格中添加一列或多列。 If we use SELECT * these columns will be dragged in automatically, which may break our code or cause it to perform badly (especially if a LOB is involved). 如果我们使用SELECT *,这些列将被自动拖入,这可能会破坏我们的代码或导致它执行不当(特别是如果涉及LOB)。

除非您从不引用客户端代码中的任何列名称,否则应使用命名列。

I'm not going to tell you to " never ever " or " always " use one or the other. 我不会告诉你“ 从来没有 ”或“ 总是 ”使用其中一个。 Advices that start with that are not to be taken literal. 从那开始的建议不是字面意思。

If you're working with small sets of data, please don't insist on using silly "optimizations" like replacing * with a list of fields, especially if you're going to specify all of the fields. 如果您正在处理小数据集,请不要坚持使用愚蠢的“优化”,例如将*替换为字段列表,特别是如果您要指定所有字段。

Having readable and easy to maintain SQL code is often worth more than a few saved CPU cycles or a couple of kilobytes less memory usage or network traffic. 具有可读且易于维护的SQL代码通常比一些节省的CPU周期或少几千字节的内存使用或网络流量更有价值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在derby jdbc中是否支持类似“从A中选择field1,field2,field3,其中(从B中选择a,b)中的(a,b)”的语法? - Is syntax like “select field1,field2,field3 from A where (a,b) in (select a,b from B)” supported in derby jdbc? Oracle PL / SQL:从已分区表的两个中间分区中选择DISTINCT FIELD1,FIELD2 - Oracle PL/SQL: SELECT DISTINCT FIELD1, FIELD2 FROM TWO ADIACENT PARTITIONS OF A PARTITIONED TABLE MS SQL:从[表]中选择field1,field2,其中,当field1 =&#39;value2&#39;时,field2 NOT IN(&#39;val1&#39;,val2&#39;) - MS SQL: SELECT field1, field2 from [table] where field2 NOT IN ('val1', val2') when field1 = 'value2' 使用“SELECT Field1,Field2,Field3 ......”代替“SELECT * ...”可以获得性能提升吗? - Is there a performance gain achieved by using “SELECT Field1, Field2, Field3 …” instead of “SELECT * …” Mysql在哪里查询…选择* where field1 + field2 + field3 &lt;=字段4 - Mysql where query… select * where field1 +field2 + field3 <= field 4 Select 其中 field1 或 field2 或 field3 等于电话号码 - Select where field1 or field2 or field3 is equal to a phone number 从单个表中检查field1和field2 - check field1 and field2 from single table 尝试在可能存在于Field1或Field2或Field3中的表中查找特定值…(依此类推) - Try to find a specific values in a table that can exist in Field1 or Field2 or Field3…(and so on) 从字段1,对应的字段2,GROUP BY字段3中查找MAX - Find MAX from field1, corresponding field2, GROUP BY field3 SELECT * FROM表WHERE字段IN(SELECT id FROM table ORDER BY field2) - SELECT * FROM table WHERE field IN (SELECT id FROM table ORDER BY field2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM