是否添加和QUOT; TOP 1 QUOT;一个SQL语句提高性能signficantly? [英] Does Adding "TOP 1" to a sql statement increase performance signficantly?

查看:514
本文介绍了是否添加和QUOT; TOP 1 QUOT;一个SQL语句提高性能signficantly?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个SQL查询,并加入TOP 1

In a SQL query, does adding "TOP 1" to

SELECT TOP 1 [values] FROM [TABLE] where [TABLE].Value = "ABC" 

给我的性能提升,当我知道,只有这些记录吗?具体来说,我正在考虑LinqToSql和方法之间的差异。单(...)。首先(...),其中。首先(...)添加 TOP 1 来生成的SQL。

give me a performance increase, when I know there is only one of those records? Specifically I'm thinking about LinqToSql and the difference between the methods .Single(...) and .First(...), where .First(...) adds the TOP 1 to the generated sql.

LinqToSql已经的感觉的慢,所以我只是试图想办法让它更快。

The LinqToSql already feels slow, so I'm just trying to think of ways to make it faster.

编辑: [TABLE] .value的也许外国在某些情况下键。

The [TABLE].Value maybe a foreign key in some instances.

推荐答案

1选择应终止一旦第一个结果的顶部发现的话,是的,它可能会快得多根据您的查询。在另一方面,你真的要考虑的语义和数据不一致未被发现的潜在含义。单()真的是最合适的,如果真的是只有一个匹配。如果你有一个以上的比赛,并使用单(),你会得到一个例外,被告知无论是在您的数据或在代码中的错误。在你的情况我会使用单()。为了使查询速度更快,我会考虑在我使用作为鉴别的列中添加索引。

A TOP 1 select should terminate once the first result is found so, yes, it could be much faster depending on your query. On the other hand, you really want to consider the semantics and the potential implication of undetected inconsistencies in the data. Single() is really most appropriate if there really is only one match. If you have more than one match and are using Single() you'll get an exception and be made aware of either the error in your data or in your code. In your case I'd use Single(). To make the query faster, I would consider adding an index on the column(s) that I'm using as the discriminator.

这篇关于是否添加和QUOT; TOP 1 QUOT;一个SQL语句提高性能signficantly?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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