在SQL Server查询中关闭NOCOUNT的优点和缺点是什么?
时间:2020-03-05 18:42:56 来源:igfitidea点击:
在SQL Server查询中关闭" NOCOUNT"有什么优点和缺点?
解决方案
回答
从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.
有关更多详细信息,请参见http://msdn.microsoft.com/zh-cn/library/ms189837.aspx。
另外,有关SQLServerCentral的这篇文章也很适合该主题:
NOCOUNT的效果
回答
而且减少的不仅仅是网络流量。 SQL Server的内部功能得到了提高,因为可以通过减少额外的查询来找出影响了多少行的方式来优化执行计划。
回答
它只是停止显示已发送/显示的行的消息,这可以提高性能,特别是如果我们有许多将返回消息的语句时。由于通过网络(在sql server和前端之间)发送的数据较少,因此可以提高性能。
BOL的更多内容:SET NOCOUNT
回答
出于上述原因,我始终将其设置为ON,但是如果我们在proc中设置了多个结果,则可能会使客户端代码混乱
回答
我个人喜欢为手动运行的查询打开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
根据我们要更新的内容的敏感性,有时包括计数是有帮助的。但是,对于具有大量输出的复杂脚本,我通常希望将其省略。