2017-07-24 107 views
0

我想知道是否有方法可以優化n提高此存儲過程的性能。我相當新的&將不勝感激您的幫助。如何優化存儲過程

這個程序我繼承了前面的程序員。

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[spDailyWorkConfB]  
    @datew DATETIME 
AS 
BEGIN 
    SET NOCOUNT ON; 

    ;WITH tmpTable AS 
    (
     SELECT IdRealizare,Adresa, CONVERT(char(2), Data, 108) as Ora 
     FROM Butoane 
     WHERE (IdRealizare <> 0) AND (DAY(butoane.Data) = DAY(@datew)) AND (MONTH(butoane.Data) = MONTH(@datew)) AND (YEAR(butoane.Data) = YEAR(@datew)) 
    ) 

    SELECT *, COUNT(*) AS Qty INTO #tblOne 
    FROM tmpTable 
    GROUP BY IdRealizare,Adresa,Ora 
    HAVING COUNT(*) >=1 
    ORDER BY Ora 

    SELECT Masini.Linie , Masini.CodMasina, Operatii.CodOperatie, Angajati.Angajat, Comenzi.NrComanda,Articole.Articol, 
      OperatiiArticol.BucatiButon,Operatii.Operatie,OperatiiArticol.BucatiOra, Masini.Grup, #tblone.Ora, #tblOne.Qty, Realizari.Id, Operatii.PozRaport, Angajati.IdSector, Realizari.LastWrite, 
      Articole.Stagione 

    INTO #tblFOR 
      FROM Realizari 
         INNER JOIN Masini ON Realizari.IdMasina = Masini.Id 
         INNER JOIN Angajati ON Realizari.IdAngajat = Angajati.Id 
         INNER JOIN Comenzi ON Realizari.IdComanda = Comenzi.Id 
         INNER JOIN Operatii ON Realizari.IdOperatie = Operatii.Id 
         INNER JOIN Articole ON Comenzi.IdArticol = Articole.Id 
         INNER JOIN OperatiiArticol ON Comenzi.IdArticol = OperatiiArticol.IdArticol AND Operatii.id = OperatiiArticol.IdOperatie  
         INNER JOIN #tblOne ON #tblOne.IdRealizare=Realizari.Id 

    SELECT * FROM #tblFOR WHERE Qty>0 AND IdSector='1' AND Linie in ('LINEA1','LINEA2','LINEA3','LINEA4','LINEA5','LINEA6','LINEA7','LINEA8','LINEA9','LINEA10','LINEA11','LINEA12') ORDER BY Realizari.Id 
END 

如果有人有一些想法,我希望聽到。 謝謝,最好的問候。

回答

0

一個提示是你應該在你的連接查詢中使用WITH(NOLOCK)。因爲這個過程基本上是爲了顯示數據而建立的。所以,當其他查詢在這些表上執行更新時,您可能會獲得未提交的數據,而不是創建死鎖。