程序不均匀时间行为

时间:2018-03-29 20:55:26

标签: sql-server-2008

调用时的过程需要3秒到5分钟。

主要目标是将前5名+所有其他成为一组。

程序是显示9种不同类型的图形,每个图形都有一个要调用的子图形。

基本功能在3-5秒内返回记录,但聚合和其他时间不可解释。

p_EnterpriseQualityPlanningGraphs ''/*@Client*/, ''/*@AuditStage*/, ''/*@Line*/, ''/*@Defect*/, ''/*
    @Area*/, ''/*@PONo*/, ''/*@Article*/, ''/*@Audited*/, ''/*@Intensity*/, ''/*@Brand*/, '01-Feb-2018'/*
    @StDate*/, '29-Mar-2018'/*@EnDate*/, 5/*@GraphName*/, ''/*@GraphClickTxt*/



ALTER Procedure p_EnterpriseQualityPlanningGraphs (@Client VarChar(32), @AuditStage VarChar(32), @Line VarChar(32), @Defect VarChar(250), 
    @Area VarChar(32), @PONo VarChar(32), @Article VarChar(32), @Audited VarChar(32), @Intensity VarChar(32), @Brand VarChar(40), 
    @StDate VarChar(11), @EnDate VarChar(32), @GraphName SmallInt, @GraphClickTxt VarChar(250))
as
Declare @DName VarChar(250)
Declare @LName VarChar(250)
Declare @DDefect VarChar(250)
If @StDate=''
Begin
    Set @StDate='01'+Right(Convert(VarChar(11), getDate(), 106), 9)
    Set @EnDate=Convert(VarChar(11), getDate(), 106)
--Select @StDate+', '+@EnDate
End
If @GraphName In (9 ) and @GraphClickTxt<>''
Begin
    Set @Defect=SubString(@GraphClickTxt, PatIndex('%<>%', @GraphClickTxt)+2, Len(@GraphClickTxt))
    Set @AuditStage=Left(@GraphClickTxt, Len(@GraphClickTxt)-(Len(@Defect)+2))
    Select @AuditStage=ID, @DName=Title From Departments Where Title=@AuditStage
    Select Top 1 @DName=@DName+' - '+ProdType From DefineProdType1_Properties Where ProdType=@Defect
    Set @DDefect=@Defect
    If @Defect='All Except Top 5' 
        Set @Defect=''
End
If @GraphName In (2,3,4,5,6) and @GraphClickTxt<>''
Begin
    Select @AuditStage=ID, @DName=Title From Departments Where Title=@GraphClickTxt
ENd
Else
If @GraphName In (7) and @GraphClickTxt<>''
Begin
--  Select @Line=ID, @LName=Title From DefLines Where Title=Left(@GraphClickTxt, PatIndex('%<>%',@GraphClickTxt)-1)
--  Select @Line=ID, @LName=Title From DefLines Where ID=Left(@GraphClickTxt, 3)
    Select @Line=ID, @LName=Title From DefLines Where Title=@GraphClickTxt
--  Set @AuditStage=SubString(@GraphClickTxt, PatIndex('%<>%',@GraphClickTxt)+2,Len(@GraphClickTxt))
--  Select @AuditStage=ID, @DName=Title From Departments Where Title=@AuditStage
ENd
Else
If @GraphName In (8) and @GraphClickTxt<>''
    Select @Line=ID From DefLines Where Title=Left(@GraphClickTxt, PatIndex('%<>%',@GraphClickTxt)-1)

Create Table #GL (ID SmallInt,
    G VarChar(250),
    L SmallInt,
    LO SmallInt,
    XAxisLabel VarChar(250),
    YAxisLabel VarChar(250)
);
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (1, 'Department Wise DR%', 4, 5, 'Departments', 'Deffect Rates');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (2, 'Line Wise DR%', 7, 0, 'Lines', 'Deffect Rates');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (3, 'Department Wise FPY%', 6, 0, 'Departments', 'FPY');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (4, @DName+' - DateWise DR%', 1, 0, '', 'Deffect Rates');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (5, @DName+' - Top Defects %', 9, 0, 'Defects', 'Deffects');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (6, @DName+' - DateWise FPY%', 0, 0, '', 'FPY');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (7, @LName+' - Date Wise DR%', 3, 0, 'Lines', 'Deffect Rates');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (8, 'Line Wise Top Defects%', 0, 0, 'Lines', 'Deffects');
Insert Into #GL (ID, G, L, LO, XAxisLabel, YAxisLabel) Values (9, @DName+' - Article Wise Defects %', 1, 0, 'Defects', 'Deffects');

Declare @TotalDefects Numeric (15,2)
If @GraphName In (5, 8, 9)
Begin
    Select @TotalDefects=Cast(Sum(T.Qty) as Numeric(15,2)) From f_MFEMatrixAuditNew ('', '', '', '', '', '', '', '', '', '', @StDate, @EnDate, @GraphName, 1) T
        Left Join #GL GL On GL.ID=@GraphName
        Where IsNull(T.GraphXAxis,'')<>'' 
        and (@Client='' or T.Client_ID=@Client)
        and (@AuditStage='' or T.AuditStage_ID=@AuditStage)
        and (@Line='' or T.Line_ID=@Line)
        and (@Defect='' or T.DefectName_Title=@Defect)
        and (@Article='' or T.ArticleNO=@Article)

    Select Max(Case When @GraphName In (9,8) or T.RowN<=5 Then T.GraphXAxis Else 'All Except Top 5' End) GraphXAxis, Max(T.GraphName) GraphName, 
        Max(T.GraphLinkWith) GraphLinkWith, Max(T.GraphLinkOther) GraphLinkOther, 
        Sum(T.GraphYAxis) GraphYAxis, Max(T.XAxisLabel) XAxisLabel, Max(T.YAxisLabel) YAxisLabel, 
        Case When @GraphName In (9,8) or Max(T.RowN)<=5 Then Max(T.StringxAxis) Else '{"country": "All Except Top 5","litres": '+Cast(Sum(T.GraphYAxis) as VarChar(40))+'}' End StringxAxis, 
        Max(T.RowN) RowN From (
        Select T.*, Row_Number() Over (Order By GraphYAxis Desc) RowN From (
            Select T.GraphXAxis, Max(GL.G) GraphName, Max(GL.L) GraphLinkWith, Max(GL.LO) GraphLinkOther, 
                Cast(Round(Sum(T.Qty)/@TotalDefects*100,2) as Numeric(15,2)) GraphYAxis, Max(GL.XAxisLabel) XAxisLabel, Max(GL.YAxisLabel) YAxisLabel,
                '{"country": "'+T.GraphXAxis+'","litres": '+Cast(Round(Sum(T.Qty)/@TotalDefects*100,2) as VarChar(40))+'}' StringxAxis
                 From f_MFEMatrixAuditNew ('', '', '', '', '', '', '', '', '', '', @StDate, @EnDate, @GraphName, 1) T
                Left Join #GL GL On GL.ID=@GraphName
                Where IsNull(T.GraphXAxis,'')<>'' 
                and (@Client='' or T.Client_ID=@Client)
                and (@AuditStage='' or T.AuditStage_ID=@AuditStage)
                and (@Line='' or T.Line_ID=@Line)
                and (@Defect='' or T.DefectName_Title=@Defect)
                and (@Article='' or T.ArticleNO=@Article)
                Group By T.GraphXAxis
            ) T
        ) T
        Where (@GraphName<>9 or (@GraphName=9 and @DDefect<>'All Except Top 5') or (@GraphName=9 and @DDefect='All Except Top 5' and T.RowN>5))
        Group By Case When @GraphName In (9,8) or T.RowN<=5 Then T.RowN Else 11 End
End
Else
    Select T.GraphXAxis, Max(GL.G) GraphName, Max(GL.L) GraphLinkWith, Max(GL.LO) GraphLinkOther, Case 
        When @GraphName In (3,6) Then Cast(Round(Sum(T.IsPassed)/Count(*)*100,2) as Numeric(15,2))
        When Sum(T.SampleSize)<>0 Then Cast(Round(Sum(T.TotalQty)/Sum(T.SampleSize)*100,2) as Numeric(15,2)) Else 0 End GraphYAxis, Max(GL.XAxisLabel) XAxisLabel, Max(GL.YAxisLabel) YAxisLabel,
        '{"country": "Pakistan","litres": 501.9}, {"country": "Other Republic","litres": 301.9}, {"country": "Ireland","litres": 201.1}, {"country": "Germany","litres": 165.8}, {"country": "Australia","litres": 139.9}, {"country": "Austria","litres": 128.3}, {"country": "UK","litres": 99}, {"country": "Belgium","litres": 60}, {"country": "The Netherlands","litres": 50}' StringxAxis/*,
        Row_Number() Over (Order By Case When @GraphName In (3,6) Then Cast(Round(Sum(T.IsPassed)/Count(*)*100,2) as Numeric(15,2))
        Else Cast(Round(Sum(T.TotalQty)/Sum(T.SampleSize)*100,2) as Numeric(15,2)) End) RowN*/
         From f_MFEMatrixAuditNew ('', '', '', '', '', '', '', '', '', '', @StDate, @EnDate, @GraphName, 0) T
        Left Join #GL GL On GL.ID=@GraphName
        Where IsNull(T.GraphXAxis,'')<>'' 
        and (@Client='' or T.Client_ID=@Client)
        and (@AuditStage='' or T.AuditStage_ID=@AuditStage)
        and (@Line='' or T.Line_ID=@Line)
        and (@Defect='' or T.DefectName_Title=@Defect)
--      and (@Article='' or T.Product_ID=@Article)
        Group By T.GraphXAxis

0 个答案:

没有答案