锁定数据库的问题

时间:2017-10-09 08:35:38

标签: sql-server sql-server-2008 sql-server-2014

我们的数据库存在很大问题。我们正在使用SQL Server 2014与2008兼容。

每天早上我们都会遇到数据库锁定问题,数据库使用100%cpu。 因此,我们每天早上修复此问题的方法是修改下面的存储过程,并在语句中添加和删除NoLock之间切换并再次运行过程,数据库很高兴。所以我们不确定为什么我们有时需要添加NoLock,有时候不需要添加。

ALTER Procedure [dbo].[ScanBox]
(
@KollieID  varchar(50) = '',
@SupplierID int = 0,
@BuyerID int = 0
)
As
Set NoCount On

IF @kollieid <> '' AND @supplierid >0 
BEGIN

    SELECT TRPO_KollieID.ID, OrderID, ISNULL(BoxNo, - 1) AS BoxNo, ISNULL(KollieID, '') AS KollieID, KollieNumber,
           ISNULL(LastStatus, - 1) AS LastStatus, LastStatusTime, ISNULL(LastStatusPDA, - 1) AS LastStatusPDA, 
           ISNULL(LastStatusTDLogin, '') AS LastStatusTDLogin, ISNULL(OrginalKollieID,'') AS OrginalKollieID,
           ISNULL(OS.StatusName, '') AS LastStatusText , ISNULL(OrderStatusExternalText,'') AS OrderStatusExternalText ,
           ISNULL(Terminal, '') AS Terminal, ISNULL(TerminalZone,'') AS TerminalZone
    FROM   TRPO_KollieID 
    WITH (NOLOCK)
           LEFT OUTER JOIN OrderStatus os WITH (NOLOCK) on OS.StatusID = TRPO_KollieID.LastStatus       
    WHERE  TRPO_KollieID.KollieID = @KollieID AND (OrderID IN (SELECT  TRPO_Header.ID  
                      FROM TRPO_Header  WITH (NOLOCK)
                           INNER JOIN TRPO_KollieID WITH (NOLOCK) ON TRPO_KollieID.OrderID = TRPO_Header.ID
                      WHERE TRPO_Header.SupplierID = @supplierid AND TRPO_Header.Status <> 'A' AND TRPO_KollieID.KollieID = @kollieid))
END
ELSE IF @kollieid <> '' AND @BuyerID >0 
BEGIN

    SELECT TRPO_KollieID.ID, OrderID, ISNULL(BoxNo, - 1) AS BoxNo, ISNULL(KollieID, '') AS KollieID, KollieNumber,
           ISNULL(LastStatus, - 1) AS LastStatus, LastStatusTime, ISNULL(LastStatusPDA, - 1) AS LastStatusPDA, 
           ISNULL(LastStatusTDLogin, '') AS LastStatusTDLogin, ISNULL(OrginalKollieID,'') AS OrginalKollieID,
           ISNULL(OS.StatusName, '') AS LastStatusText , ISNULL(OrderStatusExternalText,'') AS OrderStatusExternalText ,
           ISNULL(Terminal, '') AS Terminal, ISNULL(TerminalZone,'') AS TerminalZone
    FROM   TRPO_KollieID  
    WITH (NOLOCK)
           LEFT OUTER JOIN OrderStatus os WITH (NOLOCK) on OS.StatusID = TRPO_KollieID.LastStatus 
    WHERE  TRPO_KollieID.KollieID = @KollieID AND OrderID IN (SELECT  TRPO_Header.ID  
                      FROM TRPO_Header WITH (NOLOCK)
                           INNER JOIN TRPO_KollieID  WITH (NOLOCK) ON TRPO_KollieID.OrderID = TRPO_Header.ID
                      WHERE TRPO_Header.BuyerID = @BuyerID AND TRPO_Header.Status <> 'A' AND TRPO_KollieID.KollieID = @kollieid)    



END

我们尝试的是

  1. 我们尝试索引数据库。
  2. 尝试将isolationlevel设置为读取已提交的快照
  3. 没有什么能帮助我们。有没有人有很好的想法来解决这个问题?

2 个答案:

答案 0 :(得分:1)

重新编译可以帮助您而不是nolock。尝试添加OPTION(RECOMPILE)或OPTION(OPTIMIZE)...或者获取查询paln

答案 1 :(得分:0)

如果您使用的是Read Committed Snapshot Isolation Level,这意味着读者不会阻止编写者,编写者也不会阻止读者。所以在这种情况下nolock提示没用,我建议你删除它们。

我认为基本问题是另一个问题,但锁定,也许这个存储过程执行缓慢并使用许多服务器资源。