我有这个存储过程由SQL Server 2008R2中的定期作业调用,它总是会出现死锁:
[Microsoft] [ODBC SQL Server驱动程序] [SQL Server]事务(进程ID XX)在锁定时死锁与另一个进程通信缓冲资源并被选为死锁牺牲品。重新运行交易
程序如下,我已经替换了变量名。它获取一个参数@Time
,并将从表myTable
和视图myView
中读取数据。 myView
上没有任何索引。
此外,它还会调用另一个程序来更新另一个表mySecondTable
。
有什么建议吗? MAXDOP?视图上的索引?或问题出在光标上?
谢谢!
CREATE PROCEDURE MY_Schedule
@TIME datetime = NULL
AS
DECLARE @localVar1 varchar(50)
DECLARE @localVar2 varchar(50)
DECLARE @localVar3 int
DECLARE @localVar4 varchar(50)
DECLARE @localVar10 varchar(50)
DECLARE @localVar11 varchar(50)
DECLARE @localVar12 varchar(50)
DECLARE @localVar13 int
DECLARE @flag INT
Select @flag = Value
From myTabe
Where condition1 = ‘xxxx’ and condition2 = ‘xxxx’ and condition3 = ‘xxxx’
DECLARE MY_CURSOR CURSOR LOCAL READ_ONLY FAST_FORWARD
FOR
SELECT
P1, P2, P3, P4, P5, P6, MAX(P7) AS C1, MAX(P8) AS S1,
MAX(P9), MAX(P10), MAX(P11), MAX(P12), MAX(P13),
MAX(P14), MAX(P15)
FROM myTabe
WHERE TimeStamp BETWEEN DATEADD(DD, -1, @TIME) AND @TIME
GROUP BY P2, P3, P4, P5, P6, P1
ORDER BY P5, P6
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR
INTO @localVar1, @localVar2, @localVar3, @localVar4, @starttime, @stoptime, @localVar5, @localVar6, @localVar7, @localVar8, @localVar9, @localVar10, @localVar11, @localVar12, @localVar13
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@flag = 1 )
BEGIN
EXEC anotherPROCEDURE @localVar2, @localVar3, @localVar4,
@localVar1, @starttime, @stoptime,
@localVar13, @localVar7, @localVar8,
@localVar9, @localVar10, @localVar11,
@localVar12
END
ELSE
BEGIN
EXEC anotherPROCEDURE @localVar2, @localVar3, @localVar4,
@localVar1, @starttime, @stoptime,
@localVar6, @localVar7, @localVar8,
@localVar9, @localVar10, @localVar11,
@localVar12
END
FETCH NEXT FROM MY_CURSOR
INTO @localVar1, @localVar2, @localVar3, @localVar4, @starttime, @stoptime, @localVar5, @localVar6, @localVar7, @localVar8, @localVar9, @localVar10, @localVar11, @localVar12, @localVar13
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
答案 0 :(得分:0)
做出任何实际判断的细节太少,但我会逐行处理基于集合的方法。我重写程序以获取@TIME参数并一次处理与MY_CURSOR中相同的所有记录。从锁定(和死锁)的角度来看,它更有益。它应该快得多。