0

我的存储过程需要很长时间才能执行。任何人都可以建议我做什么来加快存储过程,除了使用一些好的做法来写下查询。我听说过创建索引,但我不确定它们是什么。请建议所有加快查询速度的最佳方法。谢谢

CREATE PROCEDURE [dbo].[usp_GetAlternates]
(
  @NNumber CHAR(11) ,
  @pid INT ,
  @pbmid INT
)
AS 
BEGIN
    TRUNCATE TABLE TempTherapeuticAlt
    INSERT  INTO TempTherapeuticAlt
            SELECT  NULL AS MedicationID ,
                    PR.ePrescribingName AS MedicationName ,
                    U.Strength AS MedicationStrength ,
                    FRM.FormName AS MedicationForm ,
                    PR.DEAClassificationID AS DEASchedule ,
                    NULL AS NDCNumber
            FROM    Product PR
                    JOIN ( SELECT   MP.MarketedProductID
                           FROM     table2 TCTSP
                                    JOIN table3 MP ON MP.SpecificProductID = TCTSP.SpecificProductID
                                    JOIN ( SELECT   TCTSP.TherapeuticConceptTreeID
                                           FROM     table3 MP
                                                    JOIN table2 TCTSP ON MP.SpecificProductID = TCTSP.SpecificProductID
                                                    JOIN ( SELECT
                                                          PR.MarketedProductID
                                                          FROM
                                                          table4 PA
                                                          JOIN Product PR ON PA.ProductID = PR.ProductID
                                                          WHERE
                                                          PA.NDC11 = @NNumber
                                                         ) PAPA ON MP.MarketedProductID = PAPA.MarketedProductID
                                         ) xxx ON TCTSP.TherapeuticConceptTreeID = xxx.TherapeuticConceptTreeID
                         ) MPI ON PR.MarketedProductID = MPI.MarketedProductID
                    JOIN ( SELECT   P.ProductID ,
                                    O.Strength ,
                                    O.Unit
                           FROM     Product AS P
                                    INNER JOIN table3 AS M ON P.MarketedProductID = M.MarketedProductID
                                    INNER JOIN table5 AS S ON M.SpecificProductID = S.SpecificProductID
                                    LEFT OUTER JOIN table6 AS O ON S.SpecificProductID = O.SpecificProductID
                           GROUP BY P.ProductID ,
                                    O.Strength ,
                                    O.Unit
                         ) U ON PR.ProductID = U.ProductID
                    JOIN ( SELECT   PA.ProductID ,
                                    S.ScriptFormID ,
                                    F.Code AS NCPDPScriptFormCode ,
                                    S.FormName
                           FROM     table4 AS PA
                                    INNER JOIN table7 AS S ON PA.NCPDPScriptFormCode = S.NCPDPScriptFormCode
                                    INNER JOIN table8 AS F ON S.FormName = F.FormName
                           GROUP BY PA.ProductID ,
                                    S.ScriptFormID ,
                                    F.Code ,
                                    S.FormName
                         ) FRM ON PR.ProductID = FRM.ProductID
            GROUP BY PR.ePrescribingName ,
                    U.Strength ,
                    FRM.FormName ,
                    PR.DEAClassificationID
            ORDER BY pr.ePrescribingName


    SELECT  LL.ProductID AS MedicationID ,
            temp.MedicationName ,
            temp.MedicationStrength ,
            temp.MedicationForm ,
            temp.DEASchedule ,
            temp.NDCNumber ,
            fs.[ReturnFormulary] AS FormularyStatus ,
            copay.CopaTier ,
            copay.FirstCopayTerm ,
            copay.FlatCopayAmount ,
            copay.PercentageCopay
    FROM    TempTherapeuticAlt temp
            OUTER APPLY ( SELECT TOP 1
                                    ProductID
                          FROM      Product
                          WHERE     ePrescribingName = temp.MedicationName
                        ) AS LL
            OUTER APPLY function1(@pid, LL.ProductID, @pbmid) AS fs
            OUTER APPLY function2(LL.ProductID, @pbmid) AS copay
    ORDER BY LL.ProductID

    TRUNCATE TABLE TempTherapeuticAlt
END

4

1 回答 1

2

这里有几个:

  1. WHERE 子句中的每一列都应该有索引。请参阅您的 SQL 语言以了解如何执行此操作。
  2. 学习如何解释计划,看看什么是慢的。
  3. 存储过程语言是功能性的,不是基于集合的。使用 JOIN 并且不要陷入 (n+1) 查询/迭代陷阱。
  4. 了解使用某些函数如何迫使您在 WHERE 子句中进行 TABLE SCAN。
于 2012-05-27T00:07:55.283 回答