查询运行速度较慢

时间:2012-10-16 03:49:18

标签: sql sql-server tsql

我有这个大规模的查询,我通常可以在2分钟内运行。然而,当我在大约一分钟之后第二次运行它时,它会无限地继续...所以我杀死了进程和我的SSMS会话。我没有在后台运行任何其他工作。

服务器上还保留了其他内容吗?我想我错过了SQL Server的工作方式。

感谢。

编辑:这是SQL(必须做一些混淆)

SELECT  pl.OrangeLocationID ,
    e.EventID ,
    cr.Title AS [Event Type] ,
    su.LastName + ', ' + su.FirstName AS FMR ,
    CONVERT(VARCHAR(20), pl.Report_Date, 101) AS [Report Entry Date] ,
    l.Name ,
    l.Number ,
    ll.SodaPopLocationID AS [SodaPop Location ID] ,
    l.Zip ,
    c.Channel ,
    pl.DT AS [ReportedDate] ,
    RIGHT(pl.DT_start, 8) AS [ReportedStartTime] ,
    RIGHT(pl.DT_end, 8) AS [ReportedEndTime] ,
    [CMS].dbo.dDateDiff(pl.DT_start, pl.DT_end) AS [ReportedDuration] ,
    pl.scheduled_date AS [ScheduledDate] ,
    RIGHT(pl.scheduled_start, 8) AS [ScheduledStartTime] ,
    RIGHT(pl.scheduled_end, 8) AS [ScheduledEndTime] ,
    [CMS].dbo.dDateDiff(pl.scheduled_start, pl.DT_end) AS [ScheduledDuration] ,
    e.HoursPaid AS [Rep Hours Worked] ,
    ISNULL(PP.[RepCount], 0) AS [RepCount] ,
    CASE WHEN [CMS].dbo.dDateDiff(pl.DT_start, pl.DT_end) = ( e.HoursPaid / ISNULL(PP.[RepCount], 1) )
         THEN [CMS].dbo.oa_HourDateDiff(pl.DT_start, pl.DT_end)
         WHEN [CMS].dbo.dDateDiff(pl.scheduled_start, pl.DT_end) = ( e.HoursPaid / ISNULL(PP.[RepCount], 1) )
         THEN [CMS].dbo.oa_HourDateDiff(pl.scheduled_start, pl.DT_end)
         ELSE ( e.HoursPaid / ISNULL(PP.[RepCount], 1) )
    END AS [FinalDuration] ,
    g.[Description] AS [OA Market] ,
    g.SodaPop_Region AS [SodaPop Region] ,
    g.SodaPop_Area AS [SodaPop Area] ,
    coup4 ,
    coupo ,
    coupo_e ,
    card_num ,
    promo ,
    promo_no ,
    promo_no_o ,
    highlight1 ,
    highlight2 ,
    highlight3 ,
    mgmt_reaction ,
    mgmt_reaction_e ,
    comm_p ,
    comm_n ,
    r.comments ,
    s_fname ,
    s_lname ,
    v_title ,
    ll.KeyAccountCorp AS [Key Account Corp.] ,
    interact_new + interact_rep AS [interact_total] ,
    samp_new + samp_rep AS [samp_total] ,
    purch_new + purch_rep AS [purch_total] ,
    23 / ( NULLIF(( interact_new + interact_rep ), 0) * 1.0 ) AS [Int_Crate] ,
    CASE WHEN sampletype = 11 THEN ( purch_new + purch_rep ) / ( NULLIF(( samp_new + samp_rep ), 0) * 1.0 )
         ELSE NULL
    END AS [Samp_Crate] ,
    coup1 + coup2 AS [coup_total] ,
    CASE WHEN coup1 + coupo > 0 THEN 1
         ELSE 0
    END AS [CoupDist] ,
    DATEPART(month, pl.DT) AS [Visit_Month] ,
    DATEPART(quarter, pl.DT) AS [Quarter] ,
    DATEPART(weekday, pl.DT) AS [Weekday] ,
    CASE DATEPART(weekday, pl.DT)
      WHEN 6 THEN 'Fri'
      WHEN 7 THEN 'Sat'
      WHEN 1 THEN 'Sun'
      ELSE 'Mon-Thurs'
    END AS [Weekday_Grouped] ,
    CASE WHEN dbo.Exception(pl.OrangeLocationID, 12) = 1
              OR dbo.Exception(pl.OrangeLocationID, 13) = 1
              OR dbo.Exception(pl.OrangeLocationID, 14) = 1 THEN 1
         ELSE 0
    END AS [EVolume] ,
    CASE WHEN dbo.DoesHaveException(pl.OrangeLocationID, 18) = 1 THEN 1
         ELSE 0
    END AS [CVolume] ,
    CASE WHEN dbo.eException(pl.OrangeLocationID, 9) = 1
              OR dbo.eException(pl.OrangeLocationID, 22) = 1 THEN 1
         ELSE 0
    END AS [Volumes] ,
    CASE WHEN dbo.eException(pl.OrangeLocationID, 8) = 1
              OR dbo.eException(pl.OrangeLocationID, 21) = 1 THEN 1
         ELSE 0
    END AS [Sales Price] ,
    CASE WHEN dbo.eException(pl.OrangeLocationID, 11) = 1 THEN 1
         ELSE 0
    END AS [Sample Volume] ,
    ISNULL(i.[NormalizedSold], 0) AS [EQBottlesSold] ,
    CASE WHEN ISNULL(purch_new, 0) = 0 THEN 0
         ELSE ISNULL(i.[NormalizedSold], 0) / ( purch_new + purch_rep )
    END AS [EQBottlesSoldPerPurch] ,
    ac.AvgSales ,
    ac.STDEVSales ,
    ( ISNULL(i.[NormalizedSold], 0) - ac.AvgSales ) / ac.STDEVSales AS [sl] ,
    ac.AvgPurchasers ,
    ac.STDEVPurchasers ,
    ( ISNULL(r.purch_new, 0) - ac.AvgPurchasers ) / ac.STrchasers AS [ZScore_Purchasers] ,  

    ac.AvgConversions ,
    ac.STDEVConversions ,
    ( ISNULL(( purch_new + purch_rep ) / ( NULLIF(( interact_new ), 0) ), 0) - ac.AvgConversions )
    / ac.STDEVConversions AS [ZScore_Conversions] ,
    ac.[AvgSalesPerPurchaser] ,
    ac.[STDEVSalesPerPurchaser] ,
    ( ISNULL(( CASE WHEN ISNULL(purch_new, 0) = 0 THEN 0
                    ELSE ISNULL(i.[NormalizedSold], 0) / ( purch_new + purch_rep )
               END ), 0) - ac.[AvgSalesPerPurchaser] ) / ac.[STDEVSalesPerPurchaser] AS [SalesPerPurchaser] ,
    ( ( ( ISNULL(i.[NormalizedSold], 0) - ac.AvgSales ) / ac.STDEVSales )
      + ( (ISNULL(( CASE WHEN ISNULL(purch_new + purch_rep, 0) = 0 THEN 0
                         ELSE ISNULL(i.[NormalizedSold], 0) / ( purch_new + purch_rep )
                    END ), 0) - ac.[AvgSalesPerPurchaser]) ) ) / 4 AS [core] ,
    ( ( (( ISNULL(i.[NormalizedSold], 0) - ac.AvgSales ) / ac.STDEVSales) ) / 4 ) + 3 AS [core] ,
    su.aaUserID ,
    l.LsocationID 
FROM [CMS_SodaPop].dbo.Schedule pl WITH ( NOLOCK )
    INNER JOIN [CMS_SodaPop].dbo.Report r WITH ( NOLOCK ) ON r.OrangeLocationID = pl.OrangeLocationID
    INNER JOIN [CMS].dbo.Users su WITH ( NOLOCK ) ON su.UserID = pl.Rep_FMR
    INNER JOIN [CMS].dbo.Locations l WITH ( NOLOCK ) ON l.LocationID = pl.LocationID
    INNER JOIN [CMS].dbo.OrangeReports cr WITH ( NOLOCK ) ON cr.RedID = pl.RedID                                                                 
    INNER JOIN [CMS_SodaPop].dbo.Events e WITH ( NOLOCK ) ON e.OrangeLocationID = pl.OrangeLocationID
    INNER JOIN [CMS_SodaPop].dbo.MarketList g WITH ( NOLOCK ) ON g.GroupID = pl.GroupID
    INNER JOIN [CMS_SodaPop].dbo.Locations ll WITH ( NOLOCK ) ON ll.LocationID = pl.LocationID
    LEFT JOIN [CMS_SodaPop].dbo.Channels c WITH ( NOLOCK ) ON ll.ChannelID = c.ChannelID
    LEFT JOIN ( SELECT  PLocationID ,
                        COUNT(DISTINCT UserID) AS [RepCount]
                FROM    [CMS_roll].dbo.rollItems WITH ( NOLOCK )
                WHERE   RedID = 154
                GROUP BY OrangeLocationID
              ) PP ON PP.OrangeLocationID = pl.OrangeLocationID
    LEFT JOIN ( SELECT  OrangeLocationID ,
                        SUM(NormalizedSold) AS [NormalizedSold]
                FROM    [Analysis].dbo.[vSodaPop_Retail_Inventory] WITH ( NOLOCK )
                GROUP BY OrangeLocationID
              ) i ON i.OrangeLocationID = pl.OrangeLocationID
    LEFT JOIN [Analysis].dbo.[vSodaPop_Calculations] ac WITH ( NOLOCK ) ON ac.[Quarter] = CASE WHEN DATEPART(MM,
                                                                                                    [DT]) IN ( 10,
                                                                                                    11, 12 ) THEN 4
                                                                                          END
                                                                           AND ac.[Year] = DATEPART(YY, pl.DT)
WHERE   pl.Activity = 1
    AND pl.RedID = 154
    AND pl.GroupID <> 444
    AND pl.[DT] < GETDATE()
    AND DATEPART(YY, [DT]) >= 2010
    AND ISNULL(i.NormalizedSold, 0) >= 0
    AND DATEPART(year, GETDATE()) = DATEPART(year, r.Insert_Date)

3 个答案:

答案 0 :(得分:1)

然而,必须要查看真正挖掘的查询..

您可以尝试在查询结尾添加OPTION(RECOMPILE)以强制它创建新的执行计划。

你在使用临时表吗?

游标未解除分配&amp;闭合?

您可以查看Profiler,看看两次执行之间是否有任何不同。

答案 1 :(得分:0)

您确定第二次没有被其他进程阻止吗?

答案 2 :(得分:0)

执行

后会发生什么
CHECKPOINT;
GO;
DBCC DROPCLEANBUFFERS;
GO;
DBCC FREEPROCCACHE;
GO;
查询之间的

?这不是真正的解决方案,但有助于诊断。