我们有一个应用程序以高请求率运行特定的SP。在SQLServer Profiler中,我看到这个SP获得了高CPU使用率,但是当我运行alter SP时(更改剂量不改变SP逻辑,它只是改为存在SP!)cpu使用率下降,并在一小时后上升再次。 我真的很困惑,为什么这个动作可以降低CPU的使用率,我该如何修复呢?
SP的定义如下:
ALTER PROCEDURE [dbo].[MySP]
@UId BigInt ,
@Ml int,
@MXL int,
@R1 int,
@R2 int,
@R3 int,
@R4 int ,
@debugmode bit
AS
If(@Ml < 1)
Set @Ml = 1
If (@MXL > 99 )
Set @MXL = 99
If (@Ml >= 42 OR @debugmode = 1)
Begin
with
tmp_one as
(
SELECT
R.Id as RID
FROM [U] as U
Inner Join
[R] as R
On R.UId = U.Id
WHERE ([R].[L] BETWEEN @Ml AND @MXL)
AND (R.UId <> @UId)
AND (0 = [U].[Status])
AND (1 = [U].[ActionStatus])
AND ((ABS((BINARY_CHECKSUM(NEWID(),R.Id)))% 10000)/100 ) BETWEEN @R1 AND (@R1 + 10)
)
Select top 10 RID
From tmp_one
Order By RID
End
Else
Begin
with
tmp_one as
(
SELECT
R.Id as RID
FROM [User] as U
Inner Join
[Rooster] as R
On R.UID = U.Id
WHERE ([R].[L] BETWEEN @Ml AND @MXL)
AND (0 = [U].[Status])
AND (1 = [U].[ActionStatus])
AND ((ABS((BINARY_CHECKSUM(R.Id,NEWID())))% 10000)/100 ) BETWEEN @R1 AND (@R1 + 10)
),
tmp_two as
(
Select tmp_one.RID as RID
From tmp_one
Where ((ABS((BINARY_CHECKSUM(RID,NEWID())))% 10000)/100 ) BETWEEN @R2 AND (@R2 + 10)
),
tmp_three as
(
Select RID as RID
From tmp_two
Where ((ABS((BINARY_CHECKSUM(NEWID())))% 10000)/100 ) BETWEEN @R3 AND (@R3 + 10)
),
tmp_four as
(
Select TOP 100 RID as RID
From tmp_three
Where ((ABS((BINARY_CHECKSUM(NEWID())))% 10000)/100 ) BETWEEN @R4 AND (@R4 + 10)
)
Select TOP(10)
RID
From tmp_four
Order By RID
End
答案 0 :(得分:0)
我在参数嗅探中发现了问题! 我通过声明局部变量并在这些变量上放置参数来修复它。
有关SQL Server参数嗅探的一些最佳文章: http://blogs.msdn.com/b/turgays/archive/2013/09/10/parameter-sniffing-problem-and-workarounds.aspx
http://blogs.technet.com/b/mdegre/archive/2012/03/19/what-is-parameter-sniffing.aspx