2

我在我的 SQL azure 数据库上遇到了一些由并发删除操作引起的死锁,我不确定如何解决它。我已将情况简化到最基本的级别。我有下表:

CREATE TABLE [dbo].[Test2013](
    [ClientID] [int] NOT NULL,
    [ID] [uniqueidentifier] NOT NULL,
    [Value] [int] NOT NULL,
 CONSTRAINT [dbo-Test2013] PRIMARY KEY CLUSTERED 
(
    [ClientID] ASC,
    [ID] ASC
)
 WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
       IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,
       ALLOW_PAGE_LOCKS  = ON)
)

GO

ALTER TABLE [dbo].[Test2013] ADD CONSTRAINT [Test2013-ID-Default-Value]
     DEFAULT (newid()) FOR [ID]
GO

导致该问题的查询如下:

INSERT INTO [Test2013]
    ([ClientID],[ID],[Value])
    SELECT
        CAST(-2147483648 AS INT) [ClientID], 
        '82ecb924-d2f0-44ee-9a8e-5240d12de088' [ID], 
        CAST(1 AS INT) [Value]

INSERT INTO [Test2013]
    ([ClientID],[ID],[Value])
    SELECT
        CAST(-2147483648 AS INT) [ClientID], 
        '82ecb924-d2f0-44ee-9a8e-5240d12de077' [ID], 
        CAST(2 AS INT) [Value]

 DECLARE @MyDateTime DATETIME
SET @MyDateTime = DATEADD(s,5,GETDATE())

DECLARE @MyDateTime2 DATETIME
SET @MyDateTime2 = DATEADD(ms,1,@MyDateTime)

BEGIN tran
    WAITFOR TIME @MyDateTime;
    DELETE FROM [Test2013]
    WHERE [ID] = '82ecb924-d2f0-44ee-9a8e-5240d12de088';
Commit tran

BEGIN tran
    WAITFOR TIME @MyDateTime2;
    DELETE FROM [Test2013]
    WHERE [ID] = '82ecb924-d2f0-44ee-9a8e-5240d12de077';
Commit tran

我认为这将是相对微不足道的,但我无法找出实际锁定查询的内容。我检查了 sys.events_log 表,它不包含任何新的死锁事件。我以前见过其他死锁,但它们都抛出了我可以处理的异常,这个只是无限期挂起。

附带说明一下,如果我将第二次操作延迟 50 毫秒,它可以正常工作。

4

1 回答 1

0

这里的挂起并不是因为死锁,而是因为你在等待一个已经过去的时间。以下是您查询的修改版本,它会在当前时间以及我运行的消息中发送垃圾邮件。如您所见,当您等待@MyDateTime2 时,它已经过去了。50 毫秒有效的原因是因为完成所有这些工作不需要 50 毫秒。我将 WAITFOR TIME 更改为 WAITFOR DELAY,它可以工作。不过,它并没有真正等待一毫秒。

INSERT INTO [Test2013]
    ([ClientID],[ID],[Value])
    SELECT
        CAST(-2147483648 AS INT) [ClientID], 
        '82ecb924-d2f0-44ee-9a8e-5240d12de088' [ID], 
        CAST(1 AS INT) [Value]

INSERT INTO [Test2013]
    ([ClientID],[ID],[Value])
    SELECT
        CAST(-2147483648 AS INT) [ClientID], 
        '82ecb924-d2f0-44ee-9a8e-5240d12de077' [ID], 
        CAST(2 AS INT) [Value]

 DECLARE @MyDateTime DATETIME
SET @MyDateTime = DATEADD(s,5,GETDATE())

PRINT(CONVERT(varchar, @MyDateTime, 121))
DECLARE @MyDateTime2 DATETIME
SET @MyDateTime2 = DATEADD(ms,1,@MyDateTime)

PRINT(CONVERT(varchar, @MyDateTime2, 121))
BEGIN tran
PRINT(CONVERT(varchar, getdate(), 121))
    WAITFOR TIME @MyDateTime;
PRINT(CONVERT(varchar, getdate(), 121))
    DELETE FROM [Test2013] 
    WHERE [ID] = '82ecb924-d2f0-44ee-9a8e-5240d12de088';
PRINT(CONVERT(varchar, getdate(), 121))
Commit tran

BEGIN tran
PRINT(CONVERT(varchar, getdate(), 121))
    WAITFOR TIME @MyDateTime2;
PRINT(CONVERT(varchar, getdate(), 121))
    DELETE FROM [Test2013]
    WHERE [ID] = '82ecb924-d2f0-44ee-9a8e-5240d12de077';
PRINT(CONVERT(varchar, getdate(), 121))
Commit tran

当我取消查询时产生的消息:

(1 row(s) affected)

(1 row(s) affected)
2013-06-21 15:13:09.980
2013-06-21 15:13:09.980
2013-06-21 15:13:04.980
2013-06-21 15:13:09.997

(1 row(s) affected)
2013-06-21 15:13:09.997
2013-06-21 15:13:10.017
Query was cancelled by user.

使用 WAITFOR DELAY 代替: INSERT INTO [Test2013] ([ClientID],[ID],[Value]) SELECT CAST(-2147483648 AS INT) [ClientID], '82ecb924-d2f0-44ee-9a8e-5240d12de088' [ID], CAST(1 AS INT) [值]

INSERT INTO [Test2013]
    ([ClientID],[ID],[Value])
    SELECT
        CAST(-2147483648 AS INT) [ClientID], 
        '82ecb924-d2f0-44ee-9a8e-5240d12de077' [ID], 
        CAST(2 AS INT) [Value]


BEGIN tran
PRINT(CONVERT(varchar, getdate(), 121))
    WAITFOR delay '00:00:05'
PRINT(CONVERT(varchar, getdate(), 121))
    DELETE FROM [Test2013] 
    WHERE [ID] = '82ecb924-d2f0-44ee-9a8e-5240d12de088';
PRINT(CONVERT(varchar, getdate(), 121))
Commit tran

BEGIN tran
PRINT(CONVERT(varchar, getdate(), 121))
    WAITFOR delay '00:00:00.001'
PRINT(CONVERT(varchar, getdate(), 121))
    DELETE FROM [Test2013]
    WHERE [ID] = '82ecb924-d2f0-44ee-9a8e-5240d12de077';
PRINT(CONVERT(varchar, getdate(), 121))
Commit tran
于 2013-06-21T20:17:09.047 回答