Sql Server查询优化技术

时间:2012-05-27 00:04:15

标签: stored-procedures query-optimization user-defined-functions indices

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

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

GO

1 个答案:

答案 0 :(得分:2)

以下是一些:

  1. 您应该为WHERE子句中的每一列都有索引。看到 你的SQL语言是如何做到的。
  2. 了解如何解析计划,看看速度有多慢。
  3. 存储过程语言功能正常,不基于设置。使用JOIN并且不要落入(n + 1)查询/迭代陷阱。
  4. 了解如何使用某些函数强制您在WHERE子句中使用TABLE SCAN。