When I declare a variable before my CTE it causes it to take about 5x longer to complete. I am on SQL Server 2008 R2.
Time in my database is stored as Unix time so I am having some variables convert from Local Time to Unix Time. For now I am only including the Unix portion. Accumulation is a running sum of production and is the reason I am using a CTE. The following portion of Code takes approximately 5 minutes to execute.
Declare @StartTime bigint
Declare @EndTime bigint
declare @ticksPerDay bigint
Set @StartTime = 635330772000000000
set @EndTime = 635357556000000000
set @ticksPerDay = 864000000000
WITH MyCTE as
(SELECT ROW_NUMBER() OVER (ORDER BY CH.countercode, CH.Time ASC) AS
Sequence, CH.PlantCode, CH.CounterCode, CH.Time, CH.Accumulation
,DateAdd(mi, DATEDIFF(mi,getutcdate(), GetDate()),
DateAdd(d,Cast((CH.Time * Power(10.00000000000,-7)/60/60/24) as
int), CAST('0001-01-01' as Date)) + Cast(ch.Time *
Power(10.00000000000,-7)/60/60/24%1 as Datetime)) as LocalTime
FROM eit.CounterHist CH
INNER JOIN eit.CounterBasic cb on ch.CounterCode = cb.CounterCode
INNER JOIN eit.LineEquipment le on cb.PlantElementCode =
le.PlantElementCode
Where CH.Time>=@StartTime -(@ticksPerDay/24) and CH.Time<=@EndTime and
le.IsCriticalMachine = 1)
SELECT c1.PlantCode, c1.CounterCode, c1.time, c1.LocalTime,
COALESCE(c1.Accumulation - c2.Accumulation, 0) AS Prod
FROM MyCTE AS c1
Inner JOIN MyCTE AS c2 ON c1.Sequence = c2.Sequence + 1
Where c1.Time>=@StartTime and c1.Time<=@EndTime
order by c1.Sequence ASC;
On the other hand, if i simply hard code the times into the where clauses instead of using variables, it takes around 1 minute to run. the code would look as follows (please look at the where clauses):
WITH MyCTE as
(SELECT ROW_NUMBER() OVER (ORDER BY CH.countercode, CH.Time ASC) AS
Sequence, CH.PlantCode, CH.CounterCode, CH.Time, CH.Accumulation,
DateAdd(mi, DATEDIFF(mi,getutcdate(), GetDate()),
DateAdd(d,Cast((CH.Time * Power(10.00000000000,-7)/60/60/24) as int),
CAST('0001-01-01' as Date)) + Cast(ch.Time *
Power(10.00000000000,-7)/60/60/24%1 as Datetime)) as LocalTime
FROM eit.CounterHist CH
INNER JOIN eit.CounterBasic cb on ch.CounterCode = cb.CounterCode
INNER JOIN eit.LineEquipment le on cb.PlantElementCode =
le.PlantElementCode
Where CH.Time>=635330772000000000 -(864000000000/24) and
CH.Time<=635357556000000000 and le.IsCriticalMachine = 1)
SELECT c1.PlantCode, c1.CounterCode, c1.time, c1.LocalTime,
COALESCE(c1.Accumulation - c2.Accumulation, 0) AS BottleCount
FROM MyCTE AS c1
Inner JOIN MyCTE AS c2 ON c1.Sequence = c2.Sequence + 1
Where c1.Time>=635330772000000000 and c1.Time<=635357556000000000
order by c1.Sequence ASC
Is there a way to use a local variable or something else in a CTE as typing in Unix time is difficult.
Edit: Front Loading more of the Code(power part) didn't really save any time. The bulk of the time save i Am seeing is in the CTE portion in the Where Clause.
Where CH.Time>=@StartTime
Where CH.Time>=635330772000000000
When i use @StartTime, it takes 5 times longer then when i hard code it in the second option.
I really want to avoid hard coding that as i want to be able to input a normal date such as '2018-03-14 06:00:00' and convert to the above time format.