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

后端 未结 7 660
故里飘歌
故里飘歌 2021-01-04 03:35

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

相关标签:
7条回答
  • 2021-01-04 04:06

    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.

    0 讨论(0)
  • 2021-01-04 04:08

    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

    0 讨论(0)
  • 2021-01-04 04:09

    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.

    0 讨论(0)
  • 2021-01-04 04:10

    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

    0 讨论(0)
  • 2021-01-04 04:11

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

    0 讨论(0)
  • 2021-01-04 04:18

    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

    0 讨论(0)
提交回复
热议问题