问题
What are the advantages and disadvantages of turning NOCOUNT
off in SQL server queries?
回答1:
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
回答2:
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.
回答3:
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
回答4:
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
回答5:
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.
回答6:
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
回答7:
Stops the message indicating the number of rows affected by a Transact-SQL statement from being returned as part of the results.
来源:https://stackoverflow.com/questions/26552/what-are-the-advantages-and-disadvantages-of-turning-nocount-off-in-sql-server-q