为什么此MERGE语句导致会话被终止?


23

我有以下MERGE针对数据库发出的声明:

MERGE "MySchema"."Point" AS t
USING (
       SELECT "ObjectId", "PointName", z."Id" AS "LocationId", i."Id" AS "Region"
         FROM @p1 AS d
         JOIN "MySchema"."Region" AS i ON i."Name" = d."Region"
    LEFT JOIN "MySchema"."Location" AS z ON z."Name" = d."Location" AND z."Region" = i."Id"
       ) AS s
   ON s."ObjectId" = t."ObjectId"
 WHEN NOT MATCHED BY TARGET 
    THEN INSERT ("ObjectId", "Name", "LocationId", "Region") VALUES (s."ObjectId", s."PointName", s."LocationId", s."Region")
 WHEN MATCHED 
    THEN UPDATE 
     SET "Name" = s."PointName"
       , "LocationId" = s."LocationId"
       , "Region" = s."Region"
OUTPUT $action, inserted.*, deleted.*;

但是,这导致会话终止,并出现以下错误:

消息0,级别11,状态0,行67。当前命令发生严重错误。结果(如有)应丢弃。

消息0,级别20,状态0,第67行当前命令发生严重错误。结果(如有)应丢弃。

我把一个简短的测试脚本放在一起会产生错误:

USE master;
GO
IF DB_ID('TEST') IS NOT NULL
DROP DATABASE "TEST";
GO
CREATE DATABASE "TEST";
GO
USE "TEST";
GO

SET NOCOUNT ON;

IF SCHEMA_ID('MySchema') IS NULL
EXECUTE('CREATE SCHEMA "MySchema"');
GO

IF OBJECT_ID('MySchema.Region', 'U') IS NULL
CREATE TABLE "MySchema"."Region" (
"Id" TINYINT IDENTITY NOT NULL CONSTRAINT "PK_MySchema_Region" PRIMARY KEY,
"Name" VARCHAR(8) NOT NULL CONSTRAINT "UK_MySchema_Region" UNIQUE
);
GO

INSERT [MySchema].[Region] ([Name]) 
VALUES (N'A'), (N'B'), (N'C'), (N'D'), (N'E'), ( N'F'), (N'G');

IF OBJECT_ID('MySchema.Location', 'U') IS NULL
CREATE TABLE "MySchema"."Location" (
"Id" SMALLINT IDENTITY NOT NULL CONSTRAINT "PK_MySchema_Location" PRIMARY KEY,
"Region" TINYINT NOT NULL CONSTRAINT "FK_MySchema_Location_Region" FOREIGN KEY REFERENCES "MySchema"."Region" ("Id"),
"Name" VARCHAR(128) NOT NULL,
CONSTRAINT "UK_MySchema_Location" UNIQUE ("Region", "Name") 
);
GO

IF OBJECT_ID('MySchema.Point', 'U') IS NULL
CREATE TABLE "MySchema"."Point" (
"ObjectId" BIGINT NOT NULL CONSTRAINT "PK_MySchema_Point" PRIMARY KEY,
"Name" VARCHAR(64) NOT NULL,
"LocationId" SMALLINT NULL CONSTRAINT "FK_MySchema_Point_Location" FOREIGN KEY REFERENCES "MySchema"."Location"("Id"),
"Region" TINYINT NOT NULL CONSTRAINT "FK_MySchema_Point_Region" FOREIGN KEY REFERENCES "MySchema"."Region" ("Id"),
CONSTRAINT "UK_MySchema_Point" UNIQUE ("Name", "Region", "LocationId")
);
GO

-- CONTAINS HISTORIC Point DATA
IF OBJECT_ID('MySchema.PointHistory', 'U') IS NULL
CREATE TABLE "MySchema"."PointHistory" (
"Id" BIGINT IDENTITY NOT NULL CONSTRAINT "PK_MySchema_PointHistory" PRIMARY KEY,
"ObjectId" BIGINT NOT NULL,
"Name" VARCHAR(64) NOT NULL,
"LocationId" SMALLINT NULL,
"Region" TINYINT NOT NULL
);
GO

CREATE TYPE "MySchema"."PointTable" AS TABLE (
"ObjectId"      BIGINT          NOT NULL PRIMARY KEY,
"PointName"     VARCHAR(64)     NOT NULL,
"Location"      VARCHAR(16)     NULL,
"Region"        VARCHAR(8)      NOT NULL,
UNIQUE ("PointName", "Region", "Location")
);
GO

DECLARE @p1 "MySchema"."PointTable";

insert into @p1 values(10001769996,N'ABCDEFGH',N'N/A',N'E')

MERGE "MySchema"."Point" AS t
USING (
       SELECT "ObjectId", "PointName", z."Id" AS "LocationId", i."Id" AS "Region"
         FROM @p1 AS d
         JOIN "MySchema"."Region" AS i ON i."Name" = d."Region"
    LEFT JOIN "MySchema"."Location" AS z ON z."Name" = d."Location" AND z."Region" = i."Id"
       ) AS s
   ON s."ObjectId" = t."ObjectId"
 WHEN NOT MATCHED BY TARGET 
    THEN INSERT ("ObjectId", "Name", "LocationId", "Region") VALUES (s."ObjectId", s."PointName", s."LocationId", s."Region")
 WHEN MATCHED 
    THEN UPDATE 
     SET "Name" = s."PointName"
       , "LocationId" = s."LocationId"
       , "Region" = s."Region"
OUTPUT $action, inserted.*, deleted.*;

如果删除该OUTPUT子句,则不会发生错误。另外,如果删除deleted引用,则不会发生错误。因此,我在MSDN文档中查看了以下OUTPUT子句:

DELETED不能与INSERT语句中的OUTPUT子句一起使用。

这对我来说很有意义,但是整个问题MERGE是您可能事先不知道。

此外,无论采取何种措施,以下脚本都可以正常工作:

USE tempdb;
GO
CREATE TABLE dbo.Target(EmployeeID int, EmployeeName varchar(10), 
     CONSTRAINT Target_PK PRIMARY KEY(EmployeeID));
CREATE TABLE dbo.Source(EmployeeID int, EmployeeName varchar(10), 
     CONSTRAINT Source_PK PRIMARY KEY(EmployeeID));
GO
INSERT dbo.Target(EmployeeID, EmployeeName) VALUES(100, 'Mary');
INSERT dbo.Target(EmployeeID, EmployeeName) VALUES(101, 'Sara');
INSERT dbo.Target(EmployeeID, EmployeeName) VALUES(102, 'Stefano');

GO
INSERT dbo.Source(EmployeeID, EmployeeName) Values(103, 'Bob');
INSERT dbo.Source(EmployeeID, EmployeeName) Values(104, 'Steve');
GO
-- MERGE statement with the join conditions specified correctly.
USE tempdb;
GO
BEGIN TRAN;
MERGE Target AS T
USING Source AS S
ON (T.EmployeeID = S.EmployeeID) 
WHEN NOT MATCHED BY TARGET AND S.EmployeeName LIKE 'S%' 
    THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
WHEN MATCHED 
    THEN UPDATE SET T.EmployeeName = S.EmployeeName
WHEN NOT MATCHED BY SOURCE AND T.EmployeeName LIKE 'S%'
    THEN DELETE 
OUTPUT $action, inserted.*, deleted.*;
ROLLBACK TRAN;
GO 

另外,我还有其他查询,它们的使用OUTPUT方式与引发错误的查询方式相同,并且它们工作得很好-它们之间的唯一区别是参与的表MERGE

这给我们的生产造成了重大问题。我已经在具有128GB RAM,12 x 2.2GHZ内核,Windows Server 2012 R2的VM和Physical上的SQL2014和SQL2016中重现了此错误。

从查询生成的估计执行计划可以在以下位置找到:

估计执行计划


1
查询可以生成估计计划吗?(而且,这不会让很多人感到震惊,但是无论如何我还是建议您使用旧的高手方法 -因为您MERGE没有这种方法HOLDLOCK,因此它不能不受比赛条件的影响,甚至还有其他错误需要考虑解决(或报告)导致此问题的原因之后。)
亚伦·伯特兰

1
它给出了具有访问冲突的堆栈转储。据我在此处展开​​堆栈时看到的信息,如果i.stack.imgur.com/f9aWa.png如果这给您造成了重大问题,则应使用Microsoft PSS提出该问题。具体来说,似乎deleted.ObjectId是引起问题的原因。OUTPUT $action, inserted.*, deleted.Name, deleted.LocationId, deleted.Region工作正常。
马丁·史密斯

1
同意马丁的观点。同时,看看是否可以通过不使用MySchema.PointTable类型而只使用裸VALUES()子句或#temp表或表变量来避免此问题USING。可能有助于隔离影响因素。
亚伦·伯特兰

感谢您的帮助,我尝试使用临时表并且发生了相同的错误。我将在产品支持下提高它-同时,我将查询重写为不使用merge,以便我们可以使产品继续运行。
布朗斯通先生

Answers:


20

这是一个错误。

它与MERGE特定的漏洞填充优化有关,这些漏洞用于避免显式的万圣节保护并消除连接,以及这些漏洞如何与其他更新计划功能进行交互。

我的文章“万圣节问题–第3部分”中有关于这些优化的详细信息。

赠品是在同一张表上的插入和合并:

计划片段

解决方法

有几种方法可以克服此优化问题,因此可以避免该错误。

  1. 使用未记录的跟踪标志强制进行明确的万圣节保护:

    OPTION (QUERYTRACEON 8692);
  2. 将该ON子句更改为:

    ON s."ObjectId" = t."ObjectId" + 0
  3. 更改表类型PointTable以将主键替换为:

    ObjectID bigint NULL UNIQUE CLUSTERED CHECK (ObjectId IS NOT NULL)

    所述CHECK约束部分是可选的,包括保存一个主密钥的原始空排斥性质。

“简单”更新查询处理(外键检查,唯一索引维护和输出列)非常复杂。使用MERGE添加了一些额外的层。结合上面提到的特定优化,您将有一个很好的方式来遇到类似这种极端情况的错误。

与一起报告的一长串错误中又增加了一个MERGE

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.