SQL Server:选择字面值比选择字段快吗?

28 浏览
0 Comments

SQL Server:选择字面值比选择字段快吗?

我曾见过有些人使用EXISTS (SELECT 1 FROM ...)而不是EXISTS (SELECT id FROM ...)作为一种优化方法,因为SQL Server可以直接返回给定的字面值,而无需查找和返回一个实际值。

SELECT(1)总是更快吗?从表中选择一个值是否需要比选择一个字面值更多的工作?

0
0 Comments

在SQL Server中,当你使用SELECT 1时,你明确地表明(给以后阅读你代码的人)你是在测试记录是否存在。即使没有性能上的提升(这是要讨论的问题),在代码的可读性和可维护性方面有所收益。这是一个很好的观点。从问题之外的角度来看,我同意这个观点。

The reason why this question arises is because there is a debate about whether selecting a literal value (such as SELECT 1) is faster than selecting a field in SQL Server. Some argue that selecting a literal value is faster because it does not require accessing the data in a table, while others argue that there is no significant difference in performance between the two methods.

这个问题之所以出现是因为关于在SQL Server中选择一个字面值(比如SELECT 1)是否比选择一个字段更快的争论。一些人认为选择一个字面值更快,因为它不需要访问表中的数据,而另一些人则认为这两种方法的性能没有明显的差异。

To address this question, it is important to understand how SQL Server processes queries. When you execute a SELECT statement, SQL Server needs to retrieve the data from the table(s) specified in the query. This involves accessing the data pages and retrieving the necessary rows and columns.

为了回答这个问题,我们需要了解SQL Server如何处理查询。当你执行一个SELECT语句时,SQL Server需要从查询中指定的表中检索数据。这涉及到访问数据页并检索所需的行和列。

Selecting a literal value like SELECT 1 does not require accessing any table data. It simply returns the value specified in the SELECT statement. This can be useful when you only need to check if a record exists, without actually retrieving any data from the table.

选择像SELECT 1这样的字面值不需要访问任何表数据。它只是返回SELECT语句中指定的值。当你只需要检查记录是否存在,而不需要从表中实际检索任何数据时,这是很有用的。

On the other hand, selecting a field like SELECT columnName requires accessing the table data. SQL Server needs to read the data pages and retrieve the specified column value for each row returned by the query. This can be slower compared to selecting a literal value, especially if the table has a large number of rows or the specified column is not indexed.

另一方面,选择一个字段,比如SELECT columnName,需要访问表数据。SQL Server需要读取数据页,并检索查询返回的每一行的指定列值。与选择一个字面值相比,这可能会更慢,特别是如果表有大量的行或指定的列没有索引。

In terms of code readability and maintainability, selecting a literal value can be beneficial. It clearly indicates the intention of the query and makes the code easier to understand for other developers. Additionally, if you need to modify the query in the future, it is easier to identify and update a literal value compared to a field name.

在代码的可读性和可维护性方面,选择一个字面值是有益的。它清楚地表明了查询的意图,并使其他开发人员更容易理解代码。此外,如果将来需要修改查询,与字段名相比,更容易识别和更新字面值。

In conclusion, while there may not be a significant performance difference between selecting a literal value and selecting a field in SQL Server, choosing a literal value can improve code readability and maintainability. It can also be useful in scenarios where you only need to check if a record exists without retrieving any data.

0
0 Comments

在这个问题中,原因是有人错误地认为在SELECT语句中选择字面值比选择字段更快,因此需要解决这个误解。解决方法是引用Microsoft的官方文档和SQL标准来证明SELECT字面值和SELECT字段的效果是相同的。通过这种方式,可以纠正错误答案并提供正确的信息。

以下是整理的

SQL Server: SELECT字面值是否比SELECT字段更快?

对于谷歌的缘故,我将更新这个问题,提供与此问题(Subquery using Exists 1 or Exists *)相同的答案,因为(目前)一个错误的答案被标记为被接受的答案。请注意,SQL标准实际上表示通过*的EXISTS与一个常量是相同的。

不是的。这个问题已经被讨论了很多次。SQL Server很聪明,它知道它被用于EXISTS,并且向系统返回NO DATA。

Microsoft的官方文档中写道:

http://technet.microsoft.com/en-us/library/ms189259.aspx?ppud=4

通过EXISTS引入的子查询的选择列表通常由一个星号(*)组成。没有必要列出列名,因为您只是测试是否存在满足子查询中指定条件的行。

还有,不相信我吗?试试运行下面的代码:

SELECT whatever
  FROM yourtable
 WHERE EXISTS( SELECT 1/0
                 FROM someothertable 
                WHERE a_valid_clause )

如果它实际上在SELECT列表中做了一些事情,那么它会抛出一个除以零的错误。但它没有。

编辑:请注意,SQL标准实际上谈到了这个问题。

ANSI SQL 1992标准,第191页 http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt

3) 情况:

a) 如果"*"只是包含在一个紧接在EXISTS谓词中的子查询中,那么选择列表等同于一个任意的字面值。

感谢跟进。接受的答案已经改变。

0
0 Comments

这篇文章讨论了在SQL Server中使用字面值还是字段进行查询的速度问题。文章中提到,使用SELECT 1SELECT *EXISTS中并没有区别,因为它们实际上并不返回行的内容,而是根据WHERE子句确定的集合是否为空。通过在查询中同时运行SET STATISTICS IO ON,可以证明这两种方法是等效的。个人偏好在EXISTS中使用SELECT *

然后,另一位参与讨论的人表示同意这种观点,他认为使用WHERE EXISTS (SELECT * FROM ...)更易读。这更符合直觉-仅仅检查记录是否存在,而不做其他操作。相比之下,WHERE EXISTS (SELECT 1 FROM ...)则显得不自然。当然,这只是主观观点。

文章没有提供明确的解决方法,因为问题本身并没有具体的解决方案。文章主要是在讨论使用字面值还是字段进行查询的性能问题,并提供了一些人的观点。

0