【问题标题】:Select View and Union and deadlocked in SQL Server 2008在 SQL Server 2008 中选择查看和联合并死锁
【发布时间】:2014-01-23 07:03:56
【问题描述】:

两个涉及公共基表的 SQL 语句使用 UNION 组合在一起。

此查询由业务对象生成,并且报告工作了一段时间。

最近,当针对 Production Datamart 运行报告时,我们开始看到以下错误

消息 1205,第 13 级,状态 2,第 1 行
事务(进程 ID 121)在线程上死锁 |与另一个进程通信缓冲区资源,并已被选为死锁牺牲品。重新运行事务。

我复制了查询并在开发中运行它,它在 PROD 中的行数大致相同,它在那里工作,但在生产中运行时,它给了我与上述相同的错误。

ALTER VIEW [dbo].[TESTDetails_CASSETS]
AS      
    SELECT      
        allocation.SeatNumber,LDMCateg.LookupDescription as SeatCategory, 
        Country.CountryName AS Country,
        CityMaster.Cityname AS City, 
        FacilityMaster.FacilityDescription AS Facility, 
        BuildingMaster.BuildingDescription AS Building, 
        FloorMaster.FloorDescription AS Floor, 
        WingMaster.WingDescription AS Wing, 
        BayMaster.BayDescription AS Bay, 
        allocation.AssociateID, allocation.AssociateName, 
        allocation.PoolID AS ProjectID, 
        allocation.PoolName AS ProjectName, 
        allocation.SeatAllocationStartDate, 
        allocation.SeatAllocationEndDate, 
        allocation.ShiftStartTime, 
        allocation.ShiftEndTime, 
        AllocAccount.PoolID as AccountID,
        AllocAccount.PoolName as AccountName, 
        AllocPRactice.PoolID AS PracticeID,
        data.SeatRequestTypeDesc AS SeatAllocationType, 
        dbo.FloorMaster.HCMLocationCode, 
        LDMNetwork.LookupDescription as Network,
        LDMSEZ.LookupDescription as FacilityType,
        LDMSECLUDED.LookupDescription as IsSeatSecluded

    FROM        
        dbo.SeatAllocation(NOLOCK) AS allocation 
    INNER JOIN
        dbo.SeatAllocation(NOLOCK) AllocPRactice on allocation.SeatNumber = AllocPRactice.SeatNumber 
    INNER JOIN
        dbo.SeatAllocation(NOLOCK) AllocAccount on allocation.SeatNumber = AllocAccount.SeatNumber 
    INNER JOIN
        dbo.SeatRequestType(NOLOCK) AS data ON allocation.SeatAllocationTypeID = data.SeatRequestTypeID 
    INNER JOIN
        dbo.SeatMaster(NOLOCK) AS SeatMr ON SeatMr.SeatNumber = allocation.SeatNumber 
    INNER JOIN
        dbo.Country(NOLOCK) ON dbo.Country.CountryID = SeatMr.CountryID 
    INNER JOIN
        dbo.CityMaster(NOLOCK) ON dbo.CityMaster.CityID = SeatMr.CityID 
    LEFT OUTER JOIN
        dbo.FacilityMaster(NOLOCK) ON dbo.FacilityMaster.FacilityID = SeatMr.FacilityID 
    LEFT OUTER JOIN
        dbo.BuildingMaster(NOLOCK) ON dbo.BuildingMaster.BuildingID = SeatMr.BuildingID 
    LEFT OUTER JOIN
        dbo.FloorMaster(NOLOCK) ON dbo.FloorMaster.FloorID = SeatMr.FloorID 
    LEFT OUTER JOIN
        dbo.WingMaster(NOLOCK) ON SeatMr.WingId = dbo.WingMaster.WingID 
    LEFT OUTER JOIN
        dbo.BayMaster(NOLOCK) ON SeatMr.BayID = dbo.BayMaster.BayID  
    INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMCateg on SeatMr.SeatCategoryID = LDMCateg.LookupTypeID 
    INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMNetwork on SeatMr.NetworkType = LDMNetwork.LookupTypeID 
    INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSEZ on FacilityMaster.FacilityType = LDMSEZ.LookupTypeID 
    INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSECLUDED on SeatMr.IsSecluded = LDMSECLUDED.LookupTypeID
    WHERE
        (allocation.AssociateID IS NOT NULL) 
        AND (allocation.SeatNumber <> '') 
        --AND (dbo.CityMaster.CityID IN (10, 4, 1))
        AND AllocPRactice.PoolType = 96
        --AND AllocAccount.PoolType = 97
        AND AllocAccount.PoolType = 99
        AND allocation.SeatAllocationTypeID = 1         
        AND LDMCateg.LookupCategoryMasterID = 13
        AND LDMNetwork.LookupCategoryMasterID = 8
        AND LDMSEZ.LookupCategoryMasterID = 12
        AND LDMSECLUDED.LookupCategoryMasterID = 6

UNION

    SELECT     
        allocation.SeatNumber,
        LDMCateg.LookupDescription as SeatCategory, 
        Country.CountryName AS Country, 
        CityMaster.Cityname AS City, 
        FacilityMaster.FacilityDescription AS Facility, 
        BuildingMaster.BuildingDescription AS Building, 
        FloorMaster.FloorDescription AS Floor, 
        WingMaster.WingDescription AS Wing, 
        BayMaster.BayDescription AS Bay, 
        allocation.AssociateID, 
        allocation.AssociateName, 
        allocation.PoolID AS ProjectID, 
        allocation.PoolName AS ProjectName, 
        allocation.SeatAllocationStartDate, 
        allocation.SeatAllocationEndDate, 
        allocation.ShiftStartTime, 
        allocation.ShiftEndTime, 
        SeatMr.AccountID as AccountID,
        SeatMr.AccountName as AccountName,
        SeatMr.BusinessUnitID AS PracticeID,
        data.SeatRequestTypeDesc AS SeatAllocationType, 
        dbo.FloorMaster.HCMLocationCode, 
        LDMNetwork.LookupDescription as Network,
        LDMSEZ.LookupDescription as FacilityType,
        LDMSECLUDED.LookupDescription as IsSeatSecluded                  
    FROM    
        dbo.SeatAllocation(NOLOCK) AS allocation INNER JOIN
        dbo.SeatRequestType(NOLOCK) AS data ON allocation.SeatAllocationTypeID = data.SeatRequestTypeID INNER JOIN
        dbo.SeatMaster(NOLOCK) AS SeatMr ON SeatMr.SeatNumber = allocation.SeatNumber INNER JOIN
        dbo.Country(NOLOCK) ON dbo.Country.CountryID = SeatMr.CountryID INNER JOIN
        dbo.CityMaster(NOLOCK) ON dbo.CityMaster.CityID = SeatMr.CityID LEFT OUTER JOIN
        dbo.FacilityMaster(NOLOCK) ON dbo.FacilityMaster.FacilityID = SeatMr.FacilityID LEFT OUTER JOIN
        dbo.BuildingMaster(NOLOCK) ON dbo.BuildingMaster.BuildingID = SeatMr.BuildingID LEFT OUTER JOIN
        dbo.FloorMaster(NOLOCK) ON dbo.FloorMaster.FloorID = SeatMr.FloorID LEFT OUTER JOIN
        dbo.WingMaster(NOLOCK) ON SeatMr.WingId = dbo.WingMaster.WingID LEFT OUTER JOIN
        dbo.BayMaster(NOLOCK) ON SeatMr.BayID = dbo.BayMaster.BayID  INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMCateg on SeatMr.SeatCategoryID = LDMCateg.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMNetwork on SeatMr.NetworkType = LDMNetwork.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSEZ on FacilityMaster.FacilityType = LDMSEZ.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSECLUDED on SeatMr.IsSecluded = LDMSECLUDED.LookupTypeID 
    WHERE     
        (allocation.AssociateID IS NOT NULL) 
        AND (allocation.SeatNumber <> '') 
        --AND (dbo.CityMaster.CityID IN (10, 4, 1))
        AND allocation.SeatAllocationTypeID = 2
        AND LDMCateg.LookupCategoryMasterID = 13
        AND LDMNetwork.LookupCategoryMasterID = 8
        AND LDMSEZ.LookupCategoryMasterID = 12
        AND LDMSECLUDED.LookupCategoryMasterID = 6

UNION

    SELECT      
        SeatMr.SeatNumber,
        LDMCateg.LookupDescription as SeatCategory, 
        Country.CountryName AS Country, 
        CityMaster.Cityname AS City, 
        FacilityMaster.FacilityDescription AS Facility, 
        BuildingMaster.BuildingDescription AS Building, 
        FloorMaster.FloorDescription AS Floor, 
        WingMaster.WingDescription AS Wing, 
        BayMaster.BayDescription AS Bay, 
        NULL AS AssociateID, 
        NULL AS AssociateName, 
        NULL AS ProjectID, 
        NULL AS ProjectName, 
        NULL AS SeatAllocationStartDate, 
        NULL AS SeatAllocationEndDate, 
        NULL AS ShiftStartTime, 
        NULL AS ShiftEndTime, 
        NULL AS AccountID, 
        NULL AS AccountName, 
        NULL AS PracticeID,
        NULL AS SeatAllocationType, 
        FloorMaster.HCMLocationCode, 
        LDMNetwork.LookupDescription as Network,
        LDMSEZ.LookupDescription as FacilityType,
        LDMSECLUDED.LookupDescription as IsSeatSecluded
    FROM              
        dbo.SeatMaster(NOLOCK) AS SeatMr INNER JOIN
        dbo.Country(NOLOCK) ON dbo.Country.CountryID = SeatMr.CountryID INNER JOIN
        dbo.CityMaster(NOLOCK) ON dbo.CityMaster.CityID = SeatMr.CityID LEFT OUTER JOIN
        dbo.FacilityMaster(NOLOCK) ON dbo.FacilityMaster.FacilityID = SeatMr.FacilityID LEFT OUTER JOIN
        dbo.BuildingMaster(NOLOCK) ON dbo.BuildingMaster.BuildingID = SeatMr.BuildingID LEFT OUTER JOIN
        dbo.FloorMaster(NOLOCK) ON dbo.FloorMaster.FloorID = SeatMr.FloorID LEFT OUTER JOIN
        dbo.WingMaster(NOLOCK) ON SeatMr.WingId = dbo.WingMaster.WingID LEFT OUTER JOIN
        dbo.BayMaster(NOLOCK) ON SeatMr.BayID = dbo.BayMaster.BayID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMCateg on SeatMr.SeatCategoryID = LDMCateg.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMNetwork on SeatMr.NetworkType = LDMNetwork.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSEZ on FacilityMaster.FacilityType = LDMSEZ.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSECLUDED on SeatMr.IsSecluded = LDMSECLUDED.LookupTypeID
    WHERE     
        (SeatMr.SeatNumber <> '') 
        --AND (dbo.CityMaster.CityID IN (10, 4, 1))
        AND SeatMr.BusinessUnitID is NULL and SeatMr.AccountID is NULL
        AND SeatMr.SeatNumber NOT IN (SELECT SeatNumber FROM SeatAllocation WHERE SeatAllocationTypeID = 2)
        AND LDMCateg.LookupCategoryMasterID = 13
        AND LDMNetwork.LookupCategoryMasterID = 8
        AND LDMSEZ.LookupCategoryMasterID = 12
        AND LDMSECLUDED.LookupCategoryMasterID = 6

UNION

    SELECT      
        SeatMr.SeatNumber,
        LDMCateg.LookupDescription as SeatCategory, 
        Country.CountryName AS Country, 
        CityMaster.Cityname AS City, 
        FacilityMaster.FacilityDescription AS Facility, 
        BuildingMaster.BuildingDescription AS Building, 
        FloorMaster.FloorDescription AS Floor, 
        WingMaster.WingDescription AS Wing, 
        BayMaster.BayDescription AS Bay, 
        NULL AS AssociateID, 
        NULL AS AssociateName, 
        NULL AS ProjectID, 
        NULL AS ProjectName, 
        NULL AS SeatAllocationStartDate, 
        NULL AS SeatAllocationEndDate, 
        NULL AS ShiftStartTime, 
        NULL AS ShiftEndTime, 
        NULL AS AccountID, 
        NULL AS AccountName, 
        Allocation.PoolID AS PracticeID,
        NULL AS SeatAllocationType, 
        FloorMaster.HCMLocationCode, 
        LDMNetwork.LookupDescription as Network,
        LDMSEZ.LookupDescription as FacilityType,
        LDMSECLUDED.LookupDescription as IsSeatSecluded
    FROM              
        dbo.SeatMaster(NOLOCK) AS SeatMr INNER JOIN
        dbo.SeatAllocation(NOLOCK) as Allocation on SeatMr.SeatNumber = Allocation.SeatNumber INNER JOIN
        dbo.Country(NOLOCK) ON dbo.Country.CountryID = SeatMr.CountryID INNER JOIN
        dbo.CityMaster(NOLOCK) ON dbo.CityMaster.CityID = SeatMr.CityID LEFT OUTER JOIN
        dbo.FacilityMaster(NOLOCK) ON dbo.FacilityMaster.FacilityID = SeatMr.FacilityID LEFT OUTER JOIN
        dbo.BuildingMaster(NOLOCK) ON dbo.BuildingMaster.BuildingID = SeatMr.BuildingID LEFT OUTER JOIN
        dbo.FloorMaster(NOLOCK) ON dbo.FloorMaster.FloorID = SeatMr.FloorID LEFT OUTER JOIN
        dbo.WingMaster(NOLOCK) ON SeatMr.WingId = dbo.WingMaster.WingID LEFT OUTER JOIN
        dbo.BayMaster(NOLOCK) ON SeatMr.BayID = dbo.BayMaster.BayID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMCateg on SeatMr.SeatCategoryID = LDMCateg.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMNetwork on SeatMr.NetworkType = LDMNetwork.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSEZ on FacilityMaster.FacilityType = LDMSEZ.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSECLUDED on SeatMr.IsSecluded = LDMSECLUDED.LookupTypeID
    WHERE     
        (SeatMr.SeatNumber <> '') 
        --AND (dbo.CityMaster.CityID IN (10, 4, 1)) 
        AND SeatMr.BusinessUnitID is Not NULL 
        and SeatMr.AccountID is NULL
        and Allocation.PoolType = 96
        AND LDMCateg.LookupCategoryMasterID = 13
        AND LDMNetwork.LookupCategoryMasterID = 8
        AND LDMSEZ.LookupCategoryMasterID = 12
        AND LDMSECLUDED.LookupCategoryMasterID = 6


UNION

    SELECT     
        SeatMr.SeatNumber,
        LDMCateg.LookupDescription as SeatCategory, 
        dbo.Country.CountryName AS Country, 
        dbo.CityMaster.Cityname AS City, 
        dbo.FacilityMaster.FacilityDescription AS Facility, 
        dbo.BuildingMaster.BuildingDescription AS Building, 
        dbo.FloorMaster.FloorDescription AS Floor, 
        dbo.WingMaster.WingDescription AS Wing, 
        dbo.BayMaster.BayDescription AS Bay, 
        NULL AS AssociateID, 
        NULL AS AssociateName, 
        NULL AS ProjectID, 
        NULL AS ProjectName, 
        NULL AS SeatAllocationStartDate, 
        NULL AS SeatAllocationEndDate, 
        NULL AS ShiftStartTime, 
        NULL AS ShiftEndTime, 
        Allocation.PoolID AS AccountID, 
        Allocation.PoolName AS AccountName, 
        AllocPractice.PoolID AS PracticeID,
        NULL AS SeatAllocationType, 
        FloorMaster.HCMLocationCode,
        LDMNetwork.LookupDescription as Network,
        LDMSEZ.LookupDescription as FacilityType,
        LDMSECLUDED.LookupDescription as IsSeatSecluded
    FROM              
        dbo.SeatMaster(NOLOCK) AS SeatMr INNER JOIN
        dbo.SeatAllocation(NOLOCK) as Allocation on SeatMr.SeatNumber = Allocation.SeatNumber INNER JOIN
        dbo.SeatAllocation AllocPractice on allocation.SeatNumber = AllocPRactice.SeatNumber INNER JOIN
        dbo.Country(NOLOCK) ON dbo.Country.CountryID = SeatMr.CountryID INNER JOIN
        dbo.CityMaster(NOLOCK) ON dbo.CityMaster.CityID = SeatMr.CityID LEFT OUTER JOIN
        dbo.FacilityMaster(NOLOCK) ON dbo.FacilityMaster.FacilityID = SeatMr.FacilityID LEFT OUTER JOIN
        dbo.BuildingMaster(NOLOCK) ON dbo.BuildingMaster.BuildingID = SeatMr.BuildingID LEFT OUTER JOIN
        dbo.FloorMaster(NOLOCK) ON dbo.FloorMaster.FloorID = SeatMr.FloorID LEFT OUTER JOIN
        dbo.WingMaster(NOLOCK) ON SeatMr.WingId = dbo.WingMaster.WingID LEFT OUTER JOIN
        dbo.BayMaster(NOLOCK) ON SeatMr.BayID = dbo.BayMaster.BayID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMCateg on SeatMr.SeatCategoryID = LDMCateg.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMNetwork on SeatMr.NetworkType = LDMNetwork.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSEZ on FacilityMaster.FacilityType = LDMSEZ.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSECLUDED on SeatMr.IsSecluded = LDMSECLUDED.LookupTypeID
    WHERE     
        (SeatMr.SeatNumber <> '') 
        --AND (dbo.CityMaster.CityID IN (10, 4, 1)) 
        AND SeatMr.BusinessUnitID IS NOT NULL 
        and SeatMr.AccountID IS NOT NULL
        --and Allocation.PoolType = 97 
        and Allocation.PoolType = 99
        AND AllocPractice.PoolType = 96
        AND SeatMr.SeatNumber NOT IN
        (
            SELECT
                alloc.SeatNumber
            FROM        
                dbo.SeatAllocation AS alloc INNER JOIN
                dbo.SeatRequestType AS data ON alloc.SeatAllocationTypeID = data.SeatRequestTypeID INNER JOIN
                dbo.SeatMaster AS SeatMr ON SeatMr.SeatNumber = alloc.SeatNumber 
            WHERE     
                alloc.PoolType = 98
        )
        AND LDMCateg.LookupCategoryMasterID = 13
        AND LDMNetwork.LookupCategoryMasterID = 8
        AND LDMSEZ.LookupCategoryMasterID = 12
        AND LDMSECLUDED.LookupCategoryMasterID = 6

UNION

    SELECT      
        ProjectAllocation.SeatNumber,
        LDMCateg.LookupDescription as SeatCategory, 
        Country.CountryName AS Country, 
        CityMaster.Cityname AS City,
        FacilityMaster.FacilityDescription AS Facility, 
        BuildingMaster.BuildingDescription AS Building, 
        FloorMaster.FloorDescription AS Floor, 
        WingMaster.WingDescription AS Wing, 
        BayMaster.BayDescription AS Bay, 
        ProjectAllocation.AssociateID, 
        ProjectAllocation.AssociateName, 
        ProjectAllocation.PoolID AS ProjectID, 
        ProjectAllocation.PoolName AS ProjectName, 
        ProjectAllocation.SeatAllocationStartDate, 
        ProjectAllocation.SeatAllocationEndDate, 
        ProjectAllocation.ShiftStartTime, 
        ProjectAllocation.ShiftEndTime,
        AccountAllocation.PoolID AccountID, 
        AccountAllocation.PoolName AccountName, 
        PracticeAllocation.PoolID PracticeID, 
        data.SeatRequestTypeDesc AS SeatAllocationType, 
        FloorMaster.HCMLocationCode,
        LDMNetwork.LookupDescription as Network,
        LDMSEZ.LookupDescription as FacilityType,
        LDMSECLUDED.LookupDescription as IsSeatSecluded

    FROM
        dbo.SeatAllocation(NOLOCK) AS ProjectAllocation INNER JOIN
        dbo.SeatMaster(NOLOCK) AS SeatMr ON SeatMr.SeatNumber = ProjectAllocation.SeatNumber INNER JOIN
        dbo.SeatAllocation(NOLOCK) AS AccountAllocation ON AccountAllocation.SeatNumber = SeatMr.SeatNumber INNER JOIN
        dbo.SeatAllocation(NOLOCK) AS PracticeAllocation ON PracticeAllocation.SeatNumber = SeatMr.SeatNumber INNER JOIN
        dbo.SeatRequestType(NOLOCK) AS data ON ProjectAllocation.SeatAllocationTypeID = data.SeatRequestTypeID INNER JOIN
        dbo.Country(NOLOCK) ON dbo.Country.CountryID = SeatMr.CountryID INNER JOIN
        dbo.CityMaster(NOLOCK) ON dbo.CityMaster.CityID = SeatMr.CityID LEFT OUTER JOIN
        dbo.FacilityMaster(NOLOCK) ON dbo.FacilityMaster.FacilityID = SeatMr.FacilityID LEFT OUTER JOIN
        dbo.BuildingMaster(NOLOCK) ON dbo.BuildingMaster.BuildingID = SeatMr.BuildingID LEFT OUTER JOIN
        dbo.FloorMaster(NOLOCK) ON dbo.FloorMaster.FloorID = SeatMr.FloorID LEFT OUTER JOIN
        dbo.WingMaster(NOLOCK) ON SeatMr.WingId = dbo.WingMaster.WingID LEFT OUTER JOIN
        dbo.BayMaster(NOLOCK) ON SeatMr.BayID = dbo.BayMaster.BayID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMCateg on SeatMr.SeatCategoryID = LDMCateg.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMNetwork on SeatMr.NetworkType = LDMNetwork.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSEZ on FacilityMaster.FacilityType = LDMSEZ.LookupTypeID INNER JOIN
        dbo.LookupDataMaster(NOLOCK) LDMSECLUDED on SeatMr.IsSecluded = LDMSECLUDED.LookupTypeID

    WHERE     
        (ProjectAllocation.AssociateID  is NULL) 
        AND (ProjectAllocation.SeatNumber <> '') 
        AND SeatMr.BusinessUnitID IS NOT NULL 
        AND SeatMr.AccountID IS NOT NULL 
        AND ProjectAllocation.PoolType = 98
        --AND AccountAllocation.PoolType = 97 
        AND AccountAllocation.PoolType = 99
        AND PracticeAllocation.PoolType = 96
        AND LDMCateg.LookupCategoryMasterID = 13
        AND LDMNetwork.LookupCategoryMasterID = 8
        AND LDMSEZ.LookupCategoryMasterID = 12
        AND LDMSECLUDED.LookupCategoryMasterID = 6
GO

有什么想法吗?

谢谢

【问题讨论】:

  • 添加查询提示:使用 (nolock) 对您认为在查询运行时不会更新的数据??
  • 你有索引吗?也许您的查询正在执行锁定整个表的表扫描。
  • @drewlander 我已经添加了 NOLOCK 问题仍然存在,不知为什么!!
  • @MarkoJuvančič 是的,表格也有索引。
  • 可能包括对您问题的查询。顺便说一句:尽量避免使用 UNION,使用 UNION ALL(当然,如果可能的话),你会获得性能。

标签: sql-server deadlock


【解决方案1】:

这不是典型的死锁,典型的死锁调试技术在这里不起作用。您在“线程|通信缓冲区资源”上死锁,而不是数据页。此外,这使得其他任何人在我们的本地计算机上进行复制和调试变得极其困难。

由于您的 SELECT 和 FROM 子句看起来几乎相同,您可以通过在 WHERE 子句中简单地使用四个 OR 来重写它。它需要对

进行左连接

WHERE (a IS NOT NULL AND b = 1) OR (a IS NOT NULL AND b = 2) OR (a IS NULL AND b = 0)

作为警告,我的方法与将您的 UNION 更改为 UNION ALL 具有相同的效果,这意味着如果某些内容满足两组标准,您将不会获得重复的行。这可能是好事,也可能是坏事,但这绝对是您应该注意的事情。

如果这对您不起作用,请继续简化它,直到您摆脱僵局。可能会查看执行计划以查找可以进行的索引更改以提高效率,从而使查询使用更少的资源来死锁。甚至可以使它成为一个表值函数,您可以在其中单独运行每个函数并将结果放入临时表中,然后返回生成的临时表。

由于这是一个奇怪的案例,您不会找到太多关于它的文档,并且像我这样的推测答案可能是您在不与 Microsoft 开立案例的情况下能够做到的最好的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多