SQL性能问题如何修改以下脚本?

时间:2018-10-19 12:01:51

标签: sql-server sql-server-2012

我编写了一个SQL查询,其中涉及INNER JOIN,LEFT OUTER JOIN,UNION和一个需要花费很长时间执行的子查询。我的SQL技能不是很好,如果有人可以帮助我如何优化以下查询以使其更快,我将不胜感激。

SQL CPU Memory configuration

SELECT 
    Brand, 
    Quantity,  
    RegionNumber,  
    FinancialGroup,
    CustomerLocationNumber,  
    RecipientCode,  
    Company,  
    Contact,  
    Address1,  
    Address2,  
    City,  
    [State],  
    Zip,  
    Country,  
    SUM(CurrentYearSales) 'CurrentYearSales', 
    SUM(PriorYearSales) 'PriorYearSales'
FROM 
    (SELECT DISTINCT  
         B.BrandDescription AS Brand,   
         (CASE WHEN ISNULL(DS.FinancialGroup,'') = '' THEN 0 ELSE  ISNULL(DS.Quantity, MB.Quantity) END) As Quantity,  
         DS.RegionNumber AS RegionNumber,  
         DA.FinancialGroup 'FinancialGroup',  
         DA.CustomerGroup2 'CustomerLocationNumber',  
         RC.RecipientCode 'RecipientCode',  
         DA.Name AS Company,  
         'ATTN: KITCHEN DEPT' AS Contact,  
         DA.Address1 'Address1',  
         DA.Address2 'Address2',  
         DA.City,  
         DA.[State],  
         DA.Zip,  
         DA.Country,  
         ISNULL(CYearSales,0) 'CurrentYearSales',  
         ISNULL(PYearSales,0) 'PriorYearSales'  
     FROM 
         DealerLocator.DealerAddress DA   
     INNER JOIN 
         DealerLocator.MailingBrand MB ON DA.DealerAddessID = MB.DealerAddessID
     INNER JOIN  
         Brand B ON B.BrandCode = MB.BrandCode  
     INNER JOIN 
         [DealerLocator].[SalesRepCustomerGroup2] SR ON DA.FinancialGroup = SR.FinancialGroup 
                                                     AND DA.CustomerGroup2 = SR.CustomerGroup2  
     INNER JOIN 
         DealerLocator.RecipientCode RC ON DA.DealerAddessID = RC.DealerAddessID  
     INNER JOIN 
         DealerLocator.BrandException BE ON BE.BrandCode = MB.BrandCode 
                                         AND BE.BrandGroupID IS NOT NULL  
     INNER JOIN 
         DealerLocator.BrandGroup BGR ON BE.BrandGroupID = BGR.BrandGroupID   
     LEFT JOIN 
         #SalesSummaryData DS ON DA.FinancialGroup = DS.FinancialGroup 
                              AND DA.CustomerGroup2 = DS.CustomerGroup2  
                              AND DS.WebSiteName = BGR.WebsiteName  
     WHERE 
         DA.IsDeleted = 0 
        --Added below condition by senthil To get only active dealer locations-INC0115761    
         AND MB.IsActive = 1
         AND (DA.IsFedExAllowed = 1 AND DA.IsMailing = 1)  
         AND DS.RegionNumber = RC.RegionNumber 
         AND RC.FinancialGroup = DA.FinancialGroup
         AND RC.CustomerGroup2 = DA.CustomerGroup2
         AND (CASE WHEN ISNULL(DS.FinancialGroup,'') = '' THEN 0 ELSE ISNULL(DS.Quantity, MB.Quantity)END) != 0  
         AND (@UserID IS NULL OR SR.SalesRepID = @UserID)  
         AND (@BrandCodes IS NULL OR EXISTS (SELECT Top 1 1 FROM @BrandData WHERE Data = MB.BrandCode))  
         AND (@FinancialGroups IS NULL OR EXISTS (SELECT Top 1 1 FROM @FinancialData WHERE Data = DA.FinancialGroup))) temp
GROUP BY
    Brand,
    Quantity,  
    RegionNumber,  
    FinancialGroup,
    CustomerLocationNumber,  
    RecipientCode,  
    Company,  
    Contact,  
    Address1,  
    Address2,  
    City,  
    [State],  
    Zip,  
    Country 

在存储过程中,此查询花费了很长时间-有人可以帮助修改它以提高性能吗?如何提高性能并避免出现超时问题?

1 个答案:

答案 0 :(得分:0)

首先我们需要您提供一些信息,这太久了,无法评论。

有些事情可能会发生,并且会降低查询速度,而上面的详细信息将有助于您集中精力:

执行后计划分析

您进行了表格扫描,可以通过建议索引来缓解

Missing Index (Impact 66.8326): CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>] ON [SalesView].[SalesDetail_History] ([CustomerGroup2]) INCLUDE ([AccountNumber],[BrandCode],[FiscalYear],[FiscalMonth],[cYMonthAmt]

然后,当您尝试更新实际的表SSDCAB等时,将在TEMP TABLES上进行大量表扫描。向临时表中添加索引可能会有所帮助,但会减慢插入临时表的速度。您只需尝试一下,看看该查询是否值得。

最后,更改您的MAXDOP和COST设置,因为此查询中可能有很多并行性,而这可能不需要。