SET NOCOUNT ON usage

前端 未结 17 1743
抹茶落季
抹茶落季 2020-11-22 05:55

Inspired by this question where there are differing views on SET NOCOUNT...

Should we use SET NOCOUNT ON for SQL Server? If not, why not?

相关标签:
17条回答
  • 2020-11-22 06:04
    SET NOCOUNT ON;
    

    This line of code is used in SQL for not returning the number rows affected in the execution of the query. If we don't require the number of rows affected, we can use this as this would help in saving memory usage and increase the speeed of execution of the query.

    0 讨论(0)
  • 2020-11-22 06:04

    SET NOCOUNT ON; Above code will stop the message generated by sql server engine to fronted result window after the DML/DDL command execution.

    Why we do it? As SQL server engine takes some resource to get the status and generate the message, it is considered as overload to the Sql server engine.So we set the noncount message on.

    0 讨论(0)
  • 2020-11-22 06:04

    if (set no count== off)

    { then it will keep data of how many records affected so reduce performance } else { it will not track the record of changes hence improve perfomace } }

    0 讨论(0)
  • 2020-11-22 06:05

    Regarding the triggers breaking NHibernate, I had that experience first-hand. Basically, when NH does an UPDATE it expects certain number of rows affected. By adding SET NOCOUNT ON to the triggers you get the number of rows back to what NH expected thereby fixing the issue. So yeah, I would definitely recommend turning it off for triggers if you use NH.

    Regarding the usage in SPs, it's a matter of personal preference. I had always turned the row count off, but then again, there are no real strong arguments either way.

    On a different note, you should really consider moving away from SP-based architecture, then you won't even have this question.

    0 讨论(0)
  • 2020-11-22 06:07

    I wanted to verify myself that 'SET NOCOUNT ON' does not save a network packet nor a roundtrip

    I used a test SQLServer 2017 on another host (I used a VM) create table ttable1 (n int); insert into ttable1 values (1),(2),(3),(4),(5),(6),(7) go create procedure procNoCount as begin set nocount on update ttable1 set n=10-n end create procedure procNormal as begin update ttable1 set n=10-n end Then I traced packets on port 1433 with the tool 'Wireshark': 'capture filter' button -> 'port 1433'

    exec procNoCount

    this is the response packet: 0000 00 50 56 c0 00 08 00 0c 29 31 3f 75 08 00 45 00 0010 00 42 d0 ce 40 00 40 06 84 0d c0 a8 32 88 c0 a8 0020 32 01 05 99 fe a5 91 49 e5 9c be fb 85 01 50 18 0030 02 b4 e6 0e 00 00 04 01 00 1a 00 35 01 00 79 00 0040 00 00 00 fe 00 00 e0 00 00 00 00 00 00 00 00 00

    exec procNormal

    this is the response packet: 0000 00 50 56 c0 00 08 00 0c 29 31 3f 75 08 00 45 00 0010 00 4f d0 ea 40 00 40 06 83 e4 c0 a8 32 88 c0 a8 0020 32 01 05 99 fe a5 91 49 e8 b1 be fb 8a 35 50 18 0030 03 02 e6 1b 00 00 04 01 00 27 00 35 01 00 ff 11 0040 00 c5 00 07 00 00 00 00 00 00 00 79 00 00 00 00 0050 fe 00 00 e0 00 00 00 00 00 00 00 00 00

    On line 40 I can see '07' which is the number of 'row(s) affected'. It is included in the response packet. No extra packet.

    It has however 13 extra bytes which could be saved, but probably not more worth it than reducing column names (e.g. 'ManagingDepartment' to 'MD')

    So I see no reason to use it for performance

    BUT As others mentioned it can break ADO.NET and I also stumbled on an issue using python: MSSQL2008 - Pyodbc - Previous SQL was not a query

    So probably a good habit still...

    0 讨论(0)
  • 2020-11-22 06:10
    • SET NOCOUNT ON- It will show "Command(s) completed successfully".
    • SET NOCOUNT OFF- it will show "(No. Of row(s) affected)".
    0 讨论(0)
提交回复
热议问题