TSQL Performance Optimization GURUS Activate

  • OK TSQL Performance GURUS...

    I need help understanding why splitting this query up performs in 8 seconds and together it performs in 40+ minutes.

    What strategies can I use to modify either or both to perform better?

    I used information from a topic entitled "Finding datetime range intersections and durations" to assist in getting start and end date ranges and durations to display.

    I received help in topic entitled "Find Datetime Range Durations" to extend this to show a case where there is a start date NOT paired up with an end date (e.g. employee is still active).

    In this case I'd like the date the query is run or parameterized date to be used as the final end date in order to calculate duration.

    Current Result: (assuming query run on 2005-08-09):

    StartTime                       EndTime                        AddressNumber Dur

    1992-06-09 00:00:00.000 1997-12-05 00:00:00.000 1169               2005

    2004-08-24 00:00:00.000 2004-11-26 00:00:00.000 1169               94

    2005-07-21 00:00:00.000 2005-08-09 00:00:00.000 1169               19

    Long Query SQL:

    set statistics profile on

    set statistics io on

    set statistics time on

    GO

    -- Value is 1 for start datetime and 0 for end datetime

    DECLARE @T2 table (StartTime datetime,EndTime datetime,AddressNumber float,Dur real)

    Declare @LimitDate datetime

    set @LimitDate = '20050809'

    INSERT INTO @T2

    SELECT  t3.Datetime AS StartTime,t3.NextDatetime AS EndTime,t3.AddressNumber,Datediff(dd,t3.Datetime,t3.NextDatetime) AS 'Dur'

    FROM (

     SELECT t1.AddressNumber , t1.HistoryDate , t1.DateType , isnull(MIN( t2.HistoryDate ),@LimitDate)

     FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1

      LEFT JOIN dbo.tsgvw_JDEEmployeeHireRehireView AS t2

     ON t1.AddressNumber = t2.AddressNumber and t2.HistoryDate > t1.HistoryDate

     WHERE  (t2.DateType = 0  or t2.HistoryDate is null) and t1.HistoryDate <= @LimitDate

     GROUP BY t1.AddressNumber , t1.HistoryDate , t1.DateType

              ) AS t3 ( AddressNumber , Datetime , value , NextDateTime )

    WHERE t3.value = 1

    GROUP BY t3.AddressNumber, t3.Value, t3.Datetime, t3.NextDatetime

    ORDER BY t3.Datetime ASC

    SELECT * From @T2 order By AddressNumber, StartTime

    Broken Up Query SQL:

    set statistics profile on

    set statistics io on

    set statistics time on

    GO

    -- Value is 1 for start datetime and 0 for end datetime

    DECLARE @T2 table (StartTime datetime,EndTime datetime,AddressNumber float,Dur real)

    Declare @LimitDate datetime

    set @LimitDate = '20050809'

    SELECT t1.AddressNumber , t1.HistoryDate as [DateTime] , t1.DateType as value , isnull(MIN( t2.HistoryDate ),@LimitDate) as NextDateTime

    into #T3

     FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1

      LEFT JOIN dbo.tsgvw_JDEEmployeeHireRehireView AS t2

     ON t1.AddressNumber = t2.AddressNumber and t2.HistoryDate > t1.HistoryDate

     WHERE  (t2.DateType = 0  or t2.HistoryDate is null) and t1.HistoryDate <= @LimitDate

     GROUP BY t1.AddressNumber , t1.HistoryDate , t1.DateType

    SELECT  t3.Datetime AS StartTime,t3.NextDatetime AS EndTime,t3.AddressNumber,Datediff(dd,t3.Datetime,t3.NextDatetime) AS 'Dur'

    FROM  #T3 t3

    WHERE t3.value = 1

    GROUP BY t3.AddressNumber, t3.Value, t3.Datetime, t3.NextDatetime

    ORDER BY t3.Datetime ASC

    HERE ARE THE STATS and EXECUTION PLAN RESULTS:

    Long Query:

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server parse and compile time:

       CPU time = 0 ms, elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    Table 'Worktable'. Scan count 5, logical reads 249, physical reads 0, read-ahead reads 0.

    Table '#2ACAAC4E'. Scan count 0, logical reads 607, physical reads 0, read-ahead reads 0.

    Table 'F060116'. Scan count 2927937, logical reads 6506118, physical reads 0, read-ahead reads 0.

    Table 'F08042'. Scan count 659, logical reads 7247023, physical reads 0, read-ahead reads 0.

    SQL Server Execution Times:

       CPU time = 16 ms,  elapsed time = 23 ms.

    Warning: Null value is eliminated by an aggregate or other SET operation.

    SQL Server Execution Times:

       CPU time = 31 ms,  elapsed time = 26 ms.

    SQL Server Execution Times:

       CPU time = 31 ms,  elapsed time = 27 ms.

    Table '#2ACAAC4E'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 8 ms.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 8 ms.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 9 ms.

    Records Affected : 606

    Records Affected : 629

    Records Affected : 629

    Broken Up Query:

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server parse and compile time:

       CPU time = 265 ms, elapsed time = 267 ms.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    Table '#T3___00000008E03D'. Scan count 0, logical reads 1, physical reads 0, read-ahead reads 0.

    Table 'F060116'. Scan count 8886, logical reads 19762, physical reads 0, read-ahead reads 0.

    Table 'F08042'. Scan count 2, logical reads 21994, physical reads 0, read-ahead reads 0.

    SQL Server Execution Times:

       CPU time = 30 ms,  elapsed time = 30 ms.

    Warning: Null value is eliminated by an aggregate or other SET operation.

    SQL Server Execution Times:

       CPU time = 31 ms,  elapsed time = 33 ms.

    SQL Server Execution Times:

       CPU time = 47 ms,  elapsed time = 34 ms.

    Table '#T3__00000008E03D'. Scan count 1, logical reads 5, physical reads 0, read-ahead reads 0.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 9 ms.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 10 ms.

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 12 ms.

    Records Affected : 676

    Records Affected : 697

    Records Affected : 697

    Long Query Execution Plan:

     


    _x0023_RowsExecutesStmtTextStmtIdNodeIdParentPhysicalOpLogicalOpArgumentDefinedValuesEstimateRowsEstimateIOEstimateCPUAvgRowSizeTotalSubtreeCostOutputListWarningsTypeParallelEstimateExecutions
    16061INSERT INTO @T2SELECT t3.Datetime AS StartTime,t3.NextDatetime AS EndTime,t3.AddressNumber,Datediff(dd,t3.Datetime,t3.NextDatetime) AS 'Dur'FROM ( SELECT t1.AddressNumber , t1.HistoryDate , t1.DateType , isnull(MIN( t2.HistoryDate ),@LimitDate) FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1 LEFT JOIN dbo.tsgvw_JDEEmployeeHireRehireView AS t2 ON t1.AddressNumber = t2.AddressNumber and t2.HistoryDate > t1.HistoryDate WHERE (t2.DateType = 0 or t2.HistoryDate is null) and t1.HistoryDate <= @LimitDate GROUP BY t1.AddressNumber , t1.HistoryDate , t1.DateType ) AS t3 ( AddressNumber , Datetime , value , NextDateTime ) WHERE t3.value = 1GROUP BY t3.AddressNumber, t3.Value, t3.Datetime, t3.NextDatetimeORDER BY t3.Datetime ASC110????10.352766???19.5778828??INSERTfalse?
    26061 |--Table Insert(OBJECT@T2), SET@T2.[Dur]=[Expr1016], @T2.[AddressNumber]=[F08042].[JWAN8], @T2.[EndTime]=[Expr1015], @T2.[StartTime]=[Expr1005]))121Table InsertInsertOBJECT@T2), SET@T2.[Dur]=[Expr1016], @T2.[AddressNumber]=[F08042].[JWAN8], @T2.[EndTime]=[Expr1015], @T2.[StartTime]=[Expr1005])?10.3527660.01675675621.03527664E-051519.5778828??PLAN_ROWfalse1
    36061 |--Top(ROWCOUNT est 0)132TopTop??10.35276601.03527668E-063519.5611153[F08042].[JWAN8], [Expr1005], [Expr1015], [Expr1016]?PLAN_ROWfalse1
    46061 |--Compute Scalar(DEFINE[Expr1016]=Convert(datediff(day, [Expr1005], [Expr1015]))))143Compute ScalarCompute ScalarDEFINE[Expr1016]=Convert(datediff(day, [Expr1005], [Expr1015])))[Expr1016]=Convert(datediff(day, [Expr1005], [Expr1015]))10.35276601.03527668E-063519.5611153[F08042].[JWAN8], [Expr1005], [Expr1015], [Expr1016]?PLAN_ROWfalse1
    56061 |--Compute Scalar(DEFINE[Expr1015]=isnull([Expr1014], [@LimitDate])))154Compute ScalarCompute ScalarDEFINE[Expr1015]=isnull([Expr1014], [@LimitDate]))[Expr1015]=isnull([Expr1014], [@LimitDate])10.35276601.03527668E-063119.5611134[F08042].[JWAN8], [Expr1005], [Expr1015]?PLAN_ROWfalse1
    66061 |--Stream Aggregate(GROUP BY[Expr1005], [F08042].[JWAN8]) DEFINE[Expr1014]=MIN([Expr1012])))165Stream AggregateAggregateGROUP BY[Expr1005], [F08042].[JWAN8])[Expr1014]=MIN([Expr1012])10.35276609.063443E-053119.5611134[F08042].[JWAN8], [Expr1005], [Expr1014]?PLAN_ROWfalse1
    76651 |--Sort(ORDER BY[Expr1005] ASC, [F08042].[JWAN8] ASC))176SortSortORDER BY[Expr1005] ASC, [F08042].[JWAN8] ASC)?15.17645070.01126126110.0001929271093119.5610218[F08042].[JWAN8], [Expr1005], [Expr1012]?PLAN_ROWfalse1
    86651 |--Filter(WHERE[Expr1011]=0 OR [Expr1012]=NULL))187FilterFilterWHERE[Expr1011]=0 OR [Expr1012]=NULL)?15.176450701.37930056E-053519.5495682[F08042].[JWAN8], [Expr1005], [Expr1012]?PLAN_ROWfalse1
    99041 |--Nested Loops(Left Outer Join, OUTER REFERENCES[F08042].[JWAN8], [Expr1005]))198Nested LoopsLeft Outer JoinOUTER REFERENCES[F08042].[JWAN8], [Expr1005])?15.673869100.0001840103213519.5495548[F08042].[JWAN8], [Expr1005], [Expr1011], [Expr1012]?PLAN_ROWfalse1
    106711 |--Filter(WHERE[Expr1004]=1 AND [Expr1005]<=[@LimitDate]))1119FilterFilterWHERE[Expr1004]=1 AND [Expr1005]<=[@LimitDate])?100.000223355048849.771011[F08042].[JWAN8], [Expr1005]?PLAN_ROWfalse1
    118731 | |--Sort(ORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC))11211SortSortORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC)?253.8125460.01126126110.00326276268849.770788[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1004], [Expr1005]?PLAN_ROWfalse1
    128731 | |--Compute Scalar(DEFINE[Expr1004]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1005]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))))11312Compute ScalarCompute ScalarDEFINE[Expr1004]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1005]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))))[Expr1004]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1005]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))253.81254602.53812541E-05849.756265[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1004], [Expr1005]?PLAN_ROWfalse1
    138731 | |--Nested Loops(Inner Join, OUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH)11413Nested LoopsInner JoinOUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH?253.81254600.0010609364812669.756239[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH]?PLAN_ROWfalse1
    1444431 | |--Sort(ORDER BY[F08042].[JWAN8] ASC))11614SortSortORDER BY[F08042].[JWAN8] ASC)?253.8125460.01126126110.00326275057729.731218[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    1544431 | | |--Clustered Index Scan(OBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0))11716Clustered Index ScanClustered Index ScanOBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0)[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]253.8125468.1538740.61842921338.772304[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    168734443 | |--Clustered Index Seek(OBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD)11814Clustered Index SeekClustered Index SeekOBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD[F060116].[YAPAST], [F060116].[YAALPH]10.0032034017.9603E-0512010.0237364825[F060116].[YAPAST], [F060116].[YAALPH]?PLAN_ROWfalse253.812546
    17411671 |--Table Spool1479Table SpoolLazy Spool??14.67386910.01675675622.84129646E-06199.778358[Expr1011], [Expr1012]?PLAN_ROWfalse3
    18394658 |--Filter(WHERE[F08042].[JWAN8]=[F08042].[JWAN8] AND [Expr1012]>[Expr1005]))14947FilterFilterWHERE[F08042].[JWAN8]=[F08042].[JWAN8] AND [Expr1012]>[Expr1005])?14.673869100.000223355048849.761592[Expr1011], [Expr1012]?PLAN_ROWfalse1
    19574434658 |--Sort(ORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC))15049SortSortORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC)?253.8125460.01126126110.00326276268849.761369[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1011], [Expr1012]?PLAN_ROWfalse1
    20574434658 |--Compute Scalar(DEFINE[Expr1011]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1012]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))))15150Compute ScalarCompute ScalarDEFINE[Expr1011]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1012]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))))[Expr1011]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1012]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))253.81254602.53812541E-05849.746845[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1011], [Expr1012]?PLAN_ROWfalse1
    21574434658 |--Nested Loops(Inner Join, OUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH)15251Nested LoopsInner JoinOUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH?253.81254600.0010609364813259.7468195[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH]?PLAN_ROWfalse1
    222923494658 |--Clustered Index Scan(OBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0))15452Clustered Index ScanClustered Index ScanOBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0)[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]253.8125468.1538740.61842921338.772304[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    235744342923494 |--Clustered Index Seek(OBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD)15552Clustered Index SeekClustered Index SeekOBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD[F060116].[YAPAST], [F060116].[YAALPH]10.0032034017.9603E-0512010.0288410634[F060116].[YAPAST], [F060116].[YAALPH]?PLAN_ROWfalse253.812546

    Broken Up Query Execution Plan:

     


    _x0023_RowsExecutesStmtTextStmtIdNodeIdParentPhysicalOpLogicalOpArgumentDefinedValuesEstimateRowsEstimateIOEstimateCPUAvgRowSizeTotalSubtreeCostOutputListWarningsTypeParallelEstimateExecutions
    16761SELECT t1.AddressNumber , t1.HistoryDate as [DateTime] , t1.DateType as value , isnull(MIN( t2.HistoryDate ),@LimitDate) as NextDateTime into #T3 FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1 LEFT JOIN dbo.tsgvw_JDEEmployeeHireRehireView AS t2 ON t1.AddressNumber = t2.AddressNumber and t2.HistoryDate > t1.HistoryDate WHERE (t2.DateType = 0 or t2.HistoryDate is null) and t1.HistoryDate <= @LimitDate GROUP BY t1.AddressNumber , t1.HistoryDate , t1.DateType110????502.942749???19.6764889??SELECT_INTOfalse?
    26761 |--Table Insert(OBJECT[#T3]), SET[#T3].[NextDateTime]=[Expr1015], [#T3].[value]=[Expr1004], [#T3].[DateTime]=[Expr1005], [#T3].[AddressNumber]=[F08042].[JWAN8]))121Table InsertInsertOBJECT[#T3]), SET[#T3].[NextDateTime]=[Expr1015], [#T3].[value]=[Expr1004], [#T3].[DateTime]=[Expr1005], [#T3].[AddressNumber]=[F08042].[JWAN8])?502.9427490.01675675620.000502942771519.6764889??PLAN_ROWfalse1
    36761 |--Top(ROWCOUNT est 0)132TopTop??502.94274905.02942748E-053519.65923[F08042].[JWAN8], [Expr1004], [Expr1005], [Expr1015]?PLAN_ROWfalse1
    46761 |--Compute Scalar(DEFINE[Expr1015]=isnull([Expr1014], [@LimitDate])))143Compute ScalarCompute ScalarDEFINE[Expr1015]=isnull([Expr1014], [@LimitDate]))[Expr1015]=isnull([Expr1014], [@LimitDate])502.94274905.02942748E-053519.6591778[F08042].[JWAN8], [Expr1004], [Expr1005], [Expr1015]?PLAN_ROWfalse1
    56761 |--Stream Aggregate(GROUP BY[F08042].[JWAN8], [Expr1005], [Expr1004]) DEFINE[Expr1014]=MIN([Expr1012])))154Stream AggregateAggregateGROUP BY[F08042].[JWAN8], [Expr1005], [Expr1004])[Expr1014]=MIN([Expr1012])502.94274900.0054957193519.6591282[F08042].[JWAN8], [Expr1004], [Expr1005], [Expr1014]?PLAN_ROWfalse1
    67601 |--Sort(ORDER BY[F08042].[JWAN8] ASC, [Expr1005] ASC, [Expr1004] ASC))165SortSortORDER BY[F08042].[JWAN8] ASC, [Expr1005] ASC, [Expr1004] ASC)?1127.51270.01126126110.01793359593519.6536331[F08042].[JWAN8], [Expr1004], [Expr1005], [Expr1012]?PLAN_ROWfalse1
    77601 |--Filter(WHERE[Expr1011]=0 OR [Expr1012]=NULL))176FilterFilterWHERE[Expr1011]=0 OR [Expr1012]=NULL)?1127.512700.001031136144719.6244373[F08042].[JWAN8], [Expr1004], [Expr1005], [Expr1012]?PLAN_ROWfalse1
    812571 |--Hash Match(Left Outer Join, HASH[F08042].[JWAN8])=([F08042].[JWAN8]), RESIDUAL[F08042].[JWAN8]=[F08042].[JWAN8] AND [Expr1012]>[Expr1005]))187Hash MatchLeft Outer JoinHASH[F08042].[JWAN8])=([F08042].[JWAN8]), RESIDUAL[F08042].[JWAN8]=[F08042].[JWAN8] AND [Expr1012]>[Expr1005])?1171.7456100.02280433664719.6234055[F08042].[JWAN8], [Expr1004], [Expr1005], [Expr1011], [Expr1012]?PLAN_ROWfalse1
    98731 |--Filter(WHERE[Expr1005]<=[@LimitDate]))1108FilterFilterWHERE[Expr1005]<=[@LimitDate])?76.216700.000121946709849.800342[F08042].[JWAN8], [Expr1004], [Expr1005]?PLAN_ROWfalse1
    108731 | |--Sort(ORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC))11110SortSortORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC)?254.0556490.01126126110.00326633919849.80022[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1004], [Expr1005]?PLAN_ROWfalse1
    118731 | |--Compute Scalar(DEFINE[Expr1004]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1005]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))))11211Compute ScalarCompute ScalarDEFINE[Expr1004]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1005]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))))[Expr1004]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1005]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))254.05564902.54055649E-05849.785692[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1004], [Expr1005]?PLAN_ROWfalse1
    128731 | |--Nested Loops(Inner Join, OUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH)11312Nested LoopsInner JoinOUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH?254.05564900.0010619525612669.785666[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH]?PLAN_ROWfalse1
    1344431 | |--Sort(ORDER BY[F08042].[JWAN8] ASC))11513SortSortORDER BY[F08042].[JWAN8] ASC)?254.0556490.01126126110.003266327729.760625[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    1444431 | | |--Clustered Index Scan(OBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0))11615Clustered Index ScanClustered Index ScanOBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0)[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]254.0556498.1812820.6192191338.800501[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    158734443 | |--Clustered Index Seek(OBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD)11713Clustered Index SeekClustered Index SeekOBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD[F060116].[YAPAST], [F060116].[YAALPH]10.0032034017.9603E-0512010.0237562247[F060116].[YAPAST], [F060116].[YAALPH]?PLAN_ROWfalse254.055649
    168731 |--Sort(ORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC))1438SortSortORDER BY[F060116].[YAALPH] ASC, [F08042].[JWEFTO] ASC, [F08042].[JWSEQ#] ASC)?254.0556490.01126126110.00326633919849.80022[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1011], [Expr1012]?PLAN_ROWfalse1
    178731 |--Compute Scalar(DEFINE[Expr1011]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1012]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))))14443Compute ScalarCompute ScalarDEFINE[Expr1011]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1012]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))))[Expr1011]=If ([F08042].[JWDTAI]='DST') then 1 else If ([F08042].[JWDTAI]='DT') then 0 else 0, [Expr1012]=[dbo].[tsgudf_JDEConvertNumberDateToDatetime](Convert(ltrim(rtrim(Convert([F08042].[JWHSTD])))))254.05564902.54055649E-05849.785692[F08042].[JWAN8], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH], [Expr1011], [Expr1012]?PLAN_ROWfalse1
    188731 |--Nested Loops(Inner Join, OUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH)14544Nested LoopsInner JoinOUTER REFERENCES[F08042].[JWAN8]) WITH PREFETCH?254.05564900.0010619525612669.785666[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#], [F060116].[YAALPH]?PLAN_ROWfalse1
    1944431 |--Sort(ORDER BY[F08042].[JWAN8] ASC))14745SortSortORDER BY[F08042].[JWAN8] ASC)?254.0556490.01126126110.003266327729.760625[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    2044431 | |--Clustered Index Scan(OBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0))14847Clustered Index ScanClustered Index ScanOBJECT[JDE_PROD].[PRODDTA].[F08042].[F08042_PK]), WHERE([F08042].[JWDTAI]='DST' OR [F08042].[JWDTAI]='DT') AND Convert(ltrim(rtrim(Convert([F08042].[JWHSTD]))))<>0)[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]254.0556498.1812820.6192191338.800501[F08042].[JWAN8], [F08042].[JWDTAI], [F08042].[JWHSTD], [F08042].[JWEFTO], [F08042].[JWSEQ#]?PLAN_ROWfalse1
    218734443 |--Clustered Index Seek(OBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD)14945Clustered Index SeekClustered Index SeekOBJECT[JDE_PROD].[PRODDTA].[F060116].[F060116_PK]), SEEK[F060116].[YAAN8]=[F08042].[JWAN8]), WHERE[F060116].[YAPAST]<>'I' AND [F060116].[YAPAST]<>'T') ORDERED FORWARD[F060116].[YAPAST], [F060116].[YAALPH]10.0032034017.9603E-0512010.0237562247[F060116].[YAPAST], [F060116].[YAALPH]?PLAN_ROWfalse254.055649

  • From the plan it seems that he wants to do an ahead optimization and sort the join it actually does the join. The second one retrieve the result from t1 and matches a row in t2 so it will be much faster

    try this

    INSERT INTO @T2

    SELECT t1.HistoryDate, isnull(MIN( t2.HistoryDate ),@LimitDate) , t1.AddressNumber , Datediff(dd,t1.HistoryDate,isnull(MIN( t2.HistoryDate ),@LimitDate) )

    FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1

      LEFT JOIN dbo.tsgvw_JDEEmployeeHireRehireView AS t2

     ON t1.AddressNumber = t2.AddressNumber and t2.HistoryDate > t1.HistoryDate

     WHERE   t1.DateType =1 and (t2.DateType = 0  or t2.HistoryDate is null) and t1.HistoryDate <= @LimitDate

    GROUP BY t1.AddressNumber , t1.HistoryDate

     

    or

    SELECT

     t1.HistoryDate,

     ( SELECT isnull(MIN(HistoryDate ),@LimitDate)

      FROM dbo.tsgvw_JDEEmployeeHireRehireView

      WHERE DateType = 0 and HistoryDate>t1.HistoryDate and AddressNumber=t1.AddressNumber)

     , t1.AddressNumber

    FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1

    WHERE   t1.DateType =1 and t1.HistoryDate <= @LimitDate --Bad query see next post : )

    this will do goog if you have an index (AddressNumber,HistoryDate)


    Kindest Regards,

    Vasc

  • the second Vasc Query should have been:

    SELECT

     t1.HistoryDate,

     isnull(( SELECT MIN(HistoryDate )

      FROM dbo.tsgvw_JDEEmployeeHireRehireView

      WHERE DateType = 0 and HistoryDate>t1.HistoryDate and AddressNumber=t1.AddressNumber) ,@LimitDate)

     , t1.AddressNumber

    FROM dbo.tsgvw_JDEEmployeeHireRehireView AS t1

    WHERE   t1.DateType =1 and t1.HistoryDate <= @LimitDate

     

    But again asuming you don't want to change the query you can check that at line 22 of the long performing query you get:

    Clustered Index Scan of 2923494 rows !!!!

    That is why your performance is killed! you need to convert that into an index Seek instead!

    It is hard without the DDL of the tables involved in the view and the view definitions to figure out  the best path without changing the query.

    For the moment you need to either use the "change the query" approach recommended by Vasc or see if there are indexes that are not being used in the view!!

    hth

     

     


    * Noel

  • Right my mistake : ) didn't check well enaugh just copy paste ...and ups there goes a LITTLE (actually big) mistake

    Ya somehow the first plan force the optimizer to do the actual join and I suspect that at fault is the outside group by (wich can be dropped) and order by

    I would like a time without those two in query


    Kindest Regards,

    Vasc

  • THANK YOU BOTH for the great suggestions and help understanding all this!

    I will be looking at both of your ideas this afternoon and let you know tomorrow what happens.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply