我們的數據庫存在嚴重問題。我們使用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
我們已經嘗試是
- 我們試圖索引數據庫。
- 試圖設置的IsolationLevel讀提交快照
沒有幫助我們。有沒有人有解決這個問題的好主意?
給我們這個查詢的執行計劃。也許你沒有以正確的方式編制數據庫索引。另外 - 不要使用NOLOCK提示。 –