SQL在所有查询中设置noCount

时间:2014-01-17 06:44:19

标签: sql stored-procedures

我有一个存储的程序如下:

@userInput int

as
declare isSuperUser int
declare IDs table(
recordID int
);

set isSuper = select isSuper from [User]
if(@isSuper = 1)
Begin
    insert into recordID select * from TableA
End
else
Begin
    insert into recordID select * from TableB
End

select * from recordID

我想在每个查询中添加“set noCount on”,所以问题是:存储过程中的性能是否等同于存储过程B?

存储程序A:

@userInput int

as
declare isSuperUser int
declare IDs table(
recordID int
);
set noCount on
set isSuper = select isSuper from [User]
if(@isSuper = 1)
Begin
    insert into recordID select * from TableA
End
else
Begin
    insert into recordID select * from TableB
End

select * from recordID
set noCount off

存储过程B:

@userInput int

as
declare isSuperUser int
declare IDs table(
recordID int
);
set noCount on
set isSuper = select isSuper from [User]
set noCount off
if(@isSuper = 1)
Begin
    set noCount on
    insert into recordID select * from TableA
    set noCount off
End
else
Begin
    set noCount on
    insert into recordID select * from TableB
    set noCount off
End
set noCount on
select * from recordID
set noCount off

1 个答案:

答案 0 :(得分:1)

SET NOCOUNT ON阻止向存储过程中的每个语句发送DONE_IN_PROC消息到客户端。对于包含多个不返回实际数据的语句的存储过程,或者对于包含Transact-SQL循环的过程,将SET NOCOUNT设置为ON可以显着提高性能,因为网络流量大大减少。

TechNet

当storedProcedure正在执行时,没有必要将数据发送回客户端,设置noCount On会提高性能,因为它会减少网络流量。 在您的商店程序B中,您将NoCount切换为关闭,这会增加网络流量。