在 SQL Server 查询中关闭 NOCOUNT 有哪些优点和缺点?
在 SQL Server 查询中关闭 NOCOUNT
有何优点和缺点?
What are the advantages and disadvantages of turning NOCOUNT
off in SQL server queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
停止将指示受 Transact-SQL 语句影响的行数的消息作为结果的一部分返回。
Stops the message indicating the number of rows affected by a Transact-SQL statement from being returned as part of the results.
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
我个人喜欢为以手动方式运行的查询打开NOCOUNT,并使用大量
Print
语句来输出调试消息。 通过这种方式,您的输出看起来不太像:而更像是
根据您正在更新的内容的敏感性,有时包含计数会很有帮助; 然而,对于具有大量输出的复杂脚本,我通常喜欢将它们排除在外。
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:And more like
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.
由于上述原因,我总是将其设置为 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
它只是停止显示影响发送/显示的行数的消息,这提供了性能优势,特别是当您有许多语句将返回该消息时。 它提高了性能,因为通过网络(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
减少的不仅仅是网络流量。 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.
来自 SQL BOL:
请参阅 http://msdn.microsoft.com/en-us/library/ms189837。 aspx 了解更多详细信息。
另外,这篇关于 SQLServerCentral 的文章在这个主题上也很精彩:
NOCOUNT 的性能影响
From SQL BOL:
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