使用 NOCOUNT 提高程序性能

MIS*_*ole 4 sql-server-2008 sql-server stored-procedures optimization

我希望提高某个程序的性能,我想从插入SET NOCOUNT ON.

我已经阅读了几篇关于这个主题的文章:

亚伦·伯特兰

SET NOCOUNt ON 提高 SQL Server SP 性能

但我不太明白的是,如果每个程序都需要一次,还是每次“开始/结束”时都需要插入它

例如,在下面的过程中 - 我是否应该插入SET NOCOUNT ON右侧并设置最后一个变量:set @PrintInfo = 'No Trip Number...

或者我是否需要SET NOCOUNT ON在程序中的每个“开始”之后插入:

Create procedure [dbo].SSIS_UpdateDriveResults
(
@RSADriveID nvarchar (50),
@ProcedureID int,
@Registered int,
@Performed  int,
...
)
as 

set @ResultError = 0
set @ResultMessage = ''

declare @Id int
declare @Name varchar(64)
declare @DriveId int
declare @ErrorMessage nvarchar(255)
...

Set @ExternalIDs = cast(@RSADriveID as nvarchar(50))
set @IsFixedSite = 'N'
set @CurrentDate = getdate()
set @UpdateWho = -1
set @PrintInfo = ' No Trip Number   ' + convert(varchar(8), @RSADriveID, 1) 

select @UpdateWho = personid from db_name.[dbo].peoplelogindetail where loginid = 'RMADMIN'

if @UpdateWho <= 0
begin
set @ResultError = 1
set @ResultMessage = 'ERROR:Unable to locate employee with login RMADMIN for inserts. ' + @PrintInfo
print @ResultMessage
return
end


--Get the account or fixed site    
select top 1 @Id = a.accountid, @Name = a.name 
from db_name.[dbo].accounts a 
where a.accountid in
    (Select accountid from db_name.[dbo].drivemaster where deleted = 0 and statusid not in (5) and driveid in
        (select driveid from db_name.[dbo].DriveShiftDetail where ShiftID = (@RSADriveID))) --ShiftID

if(@Id is null or @Id <=0 )
begin
-- See if this is a fixed site
select @Id = dm.centerid, @Name = cd.desclong 
    from db_name.[dbo].drivemaster dm 
    join db_name.[dbo].centerdetail cd on dm.centerid = cd.centerid 
    where dm.deleted = 0 and dm.statusid not in (5) and dm.driveid in
        (select driveid from db_name.[dbo].DriveShiftDetail where ShiftID = (@RSADriveID)) --ShiftID

if(@Id > 0 )
begin
    set @IsFixedSite = 'Y'
end
end

-- Locate the drive
select
@DriveId = dm.driveid,
@DriveDate = dm.fromdatetime,
@Name=case when dm.drawid>0 then cd.desclong else a.name end

from db_name.[dbo].drivemaster dm
left outer join db_name.[dbo].accounts a on a.accountid=dm.accountid
left outer join db_name.[dbo].centerdetail cd on cd.centerid=dm.centerid
where dm.deleted = 0 and dm.statusid not in (5) and dm.driveid in
    (select driveid from db_name.[dbo].DriveShiftDetail where ShiftID = (@RSADriveID))

if(@DriveId is null or @DriveId <=0 )
begin
--For Historical Drives
select top 1 @DriveId = dm.driveid, @DriveDate = dm.fromdatetime, @Name=case when dm.drawid>0 then cd.desclong else a.name end, @ShiftID = dsd.ShiftID 
from db_name.[dbo].drivemaster dm
join db_name.[dbo].DriveShiftDetail dsd on dsd.DriveID=dm.DriveID
left outer join db_name.[dbo].accounts a on a.accountid=dm.accountid
left outer join db_name.[dbo].centerdetail cd on cd.centerid=dm.centerid

where dm.deleted = 0
and dm.statusid not in (5) 
and dm.externalid like @ExternalIds
and isnumeric(dm.ExternalID)=1

order by dsd.ShiftStart asc, dsd.ShiftID asc

if(@DriveId is null or @DriveId <=0 )
begin
    set @ResultError = 1
    set @ResultMessage = 'Unable to locate drive.' + @PrintInfo
    print @ResultMessage
    return
end
end
set @PrintInfo = '  Trip Number:( ' + convert(varchar(8), @RSADriveID, 1) + ' )  Name: ' + @Name
set @PrintInfo = @PrintInfo + '  Date: ' + convert(varchar(10), @DriveDate, 101)

...

if not exists (select * from db_name.[dbo].DriveShiftActualDetail where ShiftID=@ShiftId)
begin
--Insert Missing DriveShiftActualDetail Rows
INSERT INTO db_name.[dbo].DriveShiftActualDetail
    (ShiftID,FirstTimeDonors,Registered,Voids,QNS,Deferrals,Collected,Contaminated,Cancellations,TurnAways,WalkIns,SelfDeferrals,NoShows,ShiftStart,
    ShiftEnd,HadLunch,LunchStart,LunchEnd,ActualStaff,SignupReduction,UpdateWho,UpdateWhen,UniqueKey,DonorsScheduled,MildReactions,ModReactions,SevReactions)

select
    dsd.ShiftID,0,0,0,0,0,0,0,0,0,0,0,0,dsd.ShiftStart,dsd.ShiftEnd,dsd.HasLunch,dsd.LunchStart,dsd.LunchEnd,
    isnull((select count(distinct personid) from db_name.[dbo].peoplestaffingdetail where shiftid in (select shiftid from db_name.[dbo].staffingeventshiftdetail where driveshiftid=dsd.ShiftID)),0),
    dsd.SignupReduction,dbo.HemasphereUserNo(),getdate(),newid(),dsd.DonorsScheduled,0,0,0

from db_name.[dbo].DriveShiftDetail dsd

where not exists
(
    select dsad.* from db_name.[dbo].DriveShiftActualDetail dsad where dsad.shiftid=dsd.shiftid
)
and dsd.ShiftID=@ShiftId

if(@@Error <> 0)
begin
    select @ErrorMessage = description from master.dbo.sysmessages where error = @@Error
    set @ResultError = 1
    set @ResultMessage = 'ERROR:Error Inserting the Drive Shift Actual record. ' + @PrintInfo
end 
end
Run Code Online (Sandbox Code Playgroud)

Han*_*non 9

SET NOCOUNT ON;每个过程您只需要执行一次,最好是在过程本身的主体顶部。当然,您需要在任何生成输出的语句之前使用它。

因此,例如,我会使用这样的东西作为创建过程的模板:

CREATE PROCEDURE dbo.MyProc
AS
BEGIN
    SET NOCOUNT ON;
    ....
END
GO
Run Code Online (Sandbox Code Playgroud)

在线书籍这样说SET NOCOUNT ON

停止显示受 Transact-SQL 语句或存储过程影响的行数计数的消息作为结果集的一部分返回。

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

正如我在上面概述的那样,在过程主体的开头设置这个选项可以很容易地验证语句实际上是否在过程中。

请注意,某些软件(尤其是 SQL Server 本身用于链接服务器)使用行计数功能来确定执行的 DML 是否成功。设置NOCOUNT ON可能会导致出现您意想不到的错误,并且可能难以排除故障。另请注意,@AaronBertrand的以下评论和建议:

要记住的一件事(以及我每次推荐 NOCOUNT 时都会给出的免责声明)是它可能会干扰某些技术。例如,如果您有旧的 ADO 代码(在 ASP.NET 之前),它会将 DONE_IN_PROC 消息解释为独立的结果集,因此您现有的代码可能已经有像 rs.nextRecordSet() 这样的东西来跳过它们。此外,实体框架中的某些模块(可能还有其他 ORM)依赖于这些消息来确定 DML 操作的成功与否。因此,如果您使用这些技术并且已经有工作代码,请不要盲目地将它们添加到您的所有代码中。

  • 要记住的一件事(以及每当我推荐“NOCOUNT”时我都会给出的免责声明)是它可能会干扰某些技术。例如,如果您有旧的 ADO 代码(在 ASP.NET 之前),它会将 `DONE_IN_PROC` 消息解释为独立的结果集,因此您现有的代码可能已经有类似 `rs.nextRecordSet()` 之类的内容来跳过它们。此外,实体框架中的某些模块(可能还有其他 ORM)依赖于这些消息来确定 DML 操作的成功与否。因此,如果您使用这些技术并且已经有工作代码,请不要盲目地将它们添加到您的所有代码中。 (6认同)