我在我的 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 毫秒,它可以正常工作。