在 SQL Server 查询中关闭 NOCOUNT 有哪些优点和缺点?

发布于 2024-07-04 12:53:48 字数 73 浏览 5 评论 0原文

在 SQL Server 查询中关闭 NOCOUNT 有何优点和缺点?

What are the advantages and disadvantages of turning NOCOUNT off in SQL server queries?
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

放赐 2024-07-11 12:53:54

停止将指示受 Transact-SQL 语句影响的行数的消息作为结果的一部分返回。

Stops the message indicating the number of rows affected by a Transact-SQL statement from being returned as part of the results.

凉薄对峙 2024-07-11 12:53:53

SET NOCOUNT ON 是一条单行语句,Sql 服务器将消息发送回客户端。这是针对每个进程执行的(即 .. select、insert、update、delete)。如果您避免此消息,我们可以提高数据库的整体性能,并且还减少网络流量

对于 EX:

声明 @a table(id int)

set nocount on

insert @a select 1 union select 2

set nocount off

SET NOCOUNT ON is an oneline Statement, Sql server sends message back to client.this is performed for Every Process(ie .. select,insert,update,delete).if you avoid this message we can improve overall performance for our Database and also reduce network traffic

For EX:

declare @a table(id int)

set nocount on

insert @a select 1 union select 2

set nocount off

夕色琉璃 2024-07-11 12:53:53

我个人喜欢为以手动方式运行的查询打开NOCOUNT,并使用大量Print语句来输出调试消息。 通过这种方式,您的输出看起来不太像:

Updating usernames
(287 rows updated)

Done
Updating passwords
(287 rows updated)

Done
Doing the next thing
(1127 rows updated)

Done

而更像是

Updating usernames
Done

Updating passwords
Done

Doing the next thing
Done

根据您正在更新的内容的敏感性,有时包含计数会很有帮助; 然而,对于具有大量输出的复杂脚本,我通常喜欢将它们排除在外。

I personally like to turn NOCOUNT on for queries that get run in an manual fashion and use a lot of Print statements to output debugging messages. In this way, your output would look less like:

Updating usernames
(287 rows updated)

Done
Updating passwords
(287 rows updated)

Done
Doing the next thing
(1127 rows updated)

Done

And more like

Updating usernames
Done

Updating passwords
Done

Doing the next thing
Done

Depending on the sensitivity of what you're updating, sometimes it is helpful to include the counts; however, for complex scripts with a lot of output I usually like to leave them out.

不甘平庸 2024-07-11 12:53:52

由于上述原因,我总是将其设置为 ON,但是如果您的过程中有超过 1 个结果集,则可能会弄乱客户端代码

I always have it set to ON for the reasons above, but if you have more than 1 result set in your proc it could mess up client code

稀香 2024-07-11 12:53:51

它只是停止显示影响发送/显示的行数的消息,这提供了性能优势,特别是当您有许多语句将返回该消息时。 它提高了性能,因为通过网络(sql server 和前端之间)发送的数据更少。

更多信息请参见 BOL:设置 NOCOUNT

It simply stops the message that shows the # of rows effected for being sent/displayed, which provides a performance benefit, especially if you have many statements that will return the message. It improves performance since less data is being sent over the network (between the sql server and front end).

More at BOL: SET NOCOUNT

揽清风入怀 2024-07-11 12:53:50

减少的不仅仅是网络流量。 SQL Server 内部有一个提升,因为可以通过减少计算受影响行数的额外查询来优化执行计划。

And it's not just the network traffic that is reduced. There is a boost internal to SQL Server because the execution plan can be optimized due to reduction of an extra query to figure out how many rows were affected.

懷念過去 2024-07-11 12:53:50

来自 SQL BOL:

SET NOCOUNT ON 阻止发送
发送给客户端的 DONE_IN_PROC 消息
对于存储中的每个语句
程序。 对于存储过程
包含多个不包含的语句
返回大量实际数据,设置SET
NOCOUNT 到 ON 可以提供
显着的性能提升,因为
网络流量大大减少。


请参阅 http://msdn.microsoft.com/en-us/library/ms189837。 aspx 了解更多详细信息。
另外,这篇关于 SQLServerCentral 的文章在这个主题上也很精彩:
NOCOUNT 的性能影响

From SQL BOL:

SET NOCOUNT ON prevents the sending of
DONE_IN_PROC messages to the client
for each statement in a stored
procedure. For stored procedures that
contain several statements that do not
return much actual data, setting SET
NOCOUNT to ON can provide a
significant performance boost
, because
network traffic is greatly reduced.

See http://msdn.microsoft.com/en-us/library/ms189837.aspx for more details.
Also, this article on SQLServerCentral is great on this subject:
Performance Effects of NOCOUNT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文