Details

      Description

      looking through Sentry One from the latest EOM a Forecast query stood out to me. This one is our highest by CPU but the runcount isnt terribly high and the average looks to have room for improvement. It amassed about 4 hours of run time from just over 1000 executions.

      original query:
      exec sp_executesql N'
      DECLARE @IDList VARCHAR(MAX)
      SELECT @IDList = COALESCE(@IDList +'','', '''') + CONVERT(varchar(20),ForecastId)
      FROM ( SELECT ForecastId, ROW_NUMBER() OVER (PARTITION BY EntityId, BusinessDay ORDER BY GenerationDate DESC) RowNumber
      FROM Forecasting.Forecast WITH(NOLOCK)
      WHERE EntityId = @p0 AND BusinessDay >= CONVERT(date, @p1) AND BusinessDay <= CONVERT(date, @p2) ) f
      WHERE RowNumber = 1 DECLARE @Query NVARCHAR(MAX) = N''
      SELECT i.InventoryItemId, CAST(CASE WHEN i.ServiceType IN ('' + @p3 + '') THEN 1 ELSE 0 END AS bit) AS Excluded,
      Sum(i.RawTransactionCount) AS RawTransactionCount,
      Sum(i.SystemTransactionCount) AS SystemTransactionCount,
      Sum(i.ManagerTransactionCount) AS ManagerTransactionCount,
      Sum(i.LastYearTransactionCount) AS LastYearTransactionCount
      FROM Forecasting.InventoryItemMetricDetail i WITH(NOLOCK)
      WHERE i.ForecastId IN ('' + @IDList + '') AND i.IntervalStart >= @p0 AND i.IntervalStart <= @p1
      GROUP BY i.InventoryItemId, CASE WHEN i.ServiceType IN ('' + @p3 + '') THEN 1 ELSE 0 END''
      EXEC sp_executesql @Query,N''@p0 datetime,@p1 datetime'',@p0=@p1,@p1=@p2',N'@p0 bigint,@p1 datetime,@p2 datetime,@p3 nvarchar(4000)'
      ,@p0=192,@p1='2020-04-30 15:15:00',@p2='2020-05-08 08:30:00',@p3=N'1,2'

      Schalk suggested we remove the in clause and replace with a temp table as below:

      CREATE TABLE #tmpForecast(ForecastID BIGINT)
      INSERT INTO #tmpForecast (ForecastID)
      SELECT ForecastID
      FROM (
      SELECT ForecastID, ROW_NUMBER() OVER (PARTITION BY EntityID, BusinessDay ORDER BY GenerationDate DESC) RowNumber
      FROM Forecasting.Forecast WITH (NOLOCK)
      WHERE EntityID = 192
      AND BusinessDay BETWEEN '2020-04-30 15:15:00' AND '2020-05-08 08:30:00') f
      WHERE RowNumber = 1

      SELECT i.InventoryItemID, CAST(CASE WHEN i.ServiceType IN (1, 2) THEN 1 ELSE 0 END AS BIT) AS Excluded,
      SUM(i.RawTransactionCount) AS RawTransactionCount,
      SUM(i.SystemTransactionCount) AS SystemTransactionCount,
      SUM(i.ManagerTransactionCount) AS ManagerTransactionCount,
      SUM(i.LastYearTransactionCount) AS LastYearTransactionCount
      FROM Forecasting.InventoryItemMetricDetail i WITH (NOLOCK)
      INNER JOIN #tmpForecast f ON i.ForecastId = f.ForecastID
      --WHERE IN (6001224,6005391,6009586,6013947,6018380,6022699,6027063,6031384,6035708)
      AND i.IntervalStart BETWEEN '2020-04-30 15:15:00' AND '2020-05-08 08:30:00'
      GROUP BY i.InventoryItemID, CASE WHEN i.ServiceType IN (1, 2) THEN 1 ELSE 0 END

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Corey.Amend Corey Amend (Inactive)
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  PagerDuty

                  Error rendering 'com.pagerduty.jira-server-plugin:PagerDuty'. Please contact your Jira administrators.