如何优化存储过程

时间:2017-07-24 14:58:26

标签: sql-server-2008 stored-procedures

我想知道是否有方法可以优化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

如果有人有一些想法,我想听听。 谢谢你,最好的问候。

1 个答案:

答案 0 :(得分:0)

一个提示是您应该在连接查询中使用WITH(NOLOCK)。由于此过程基本上构建为显示数据所以最好是您可以获得未提交的数据,而不是在其他查询对这些表执行更新时创建死锁。