I am working with this code in SQL Query Analyzer.
This query runs against a database table with over 1.8 million records. The time frame of the query, however covers only a short period and should look at only 2000 to 5000 records (at least that is my intent). The "enddate" field is a 10-digit integer (epoch time in seconds since 1970) and is indexed (non-clustered).
This query is used three times as subquery code in a larger query that requires about 2min 45sec to run unless I use the code listed below that "runs instantly". In that case even the complex query executes within 1 second.
Here are the attempts I have made so far and the resulting execution times of the single select query. The attempts to give a starting value to "enddate" with an expression do not seem to work, only when a hard-coded value is entered does it execute quickly. I have also tried
for @StartPoint.
Your observations and expertise will certainly be appreciated.
WinN
This query runs against a database table with over 1.8 million records. The time frame of the query, however covers only a short period and should look at only 2000 to 5000 records (at least that is my intent). The "enddate" field is a 10-digit integer (epoch time in seconds since 1970) and is indexed (non-clustered).
This query is used three times as subquery code in a larger query that requires about 2min 45sec to run unless I use the code listed below that "runs instantly". In that case even the complex query executes within 1 second.
Here are the attempts I have made so far and the resulting execution times of the single select query. The attempts to give a starting value to "enddate" with an expression do not seem to work, only when a hard-coded value is entered does it execute quickly. I have also tried
Code:
Cast(datediff(ss, '01/01/1970', getutcdate())-3800, int)
Your observations and expertise will certainly be appreciated.
WinN
Code:
************* This query takes 36 seconds to execute ********/
SELECT InteractionTrack.workgroupid, Sum(InteractionTrack.answer) AS HandledCalls,
sum(queueduration)/Sum(InteractionTrack.answer) as AvgQueTime
FROM dbo.interactiontrack
WHERE
enddate BETWEEN
(CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) - 1800
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) END )
AND
CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600)
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) + 1800 END
and workgroupid >0
group by workgroupid
/****************************** This Query takes 30 seconds to execute *******************/
SELECT InteractionTrack.workgroupid, Sum(InteractionTrack.answer) AS HandledCalls,
sum(queueduration)/Sum(InteractionTrack.answer) as AvgQueTime
FROM dbo.interactiontrack
WHERE
enddate > datediff(ss, '01/01/1970', getutcdate())-3800
and
enddate
BETWEEN
(CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) - 1800
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) END )
AND
CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600)
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) + 1800 END
and workgroupid >0
group by workgroupid
/********************************** This query takes 32 seconds to execute ***************************/
Declare @StartPoint int
set @StartPoint = datediff(ss, '01/01/1970', getutcdate())-3800
SELECT InteractionTrack.workgroupid, Sum(InteractionTrack.answer) AS HandledCalls,
sum(queueduration)/Sum(InteractionTrack.answer) as AvgQueTime
FROM dbo.interactiontrack
WHERE
enddate > @StartPoint
and
enddate
BETWEEN
(CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) - 1800
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) END )
AND
CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600)
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) + 1800 END
and workgroupid >0
group by workgroupid
/************************************ This query executes instantly **************************/
Declare @StartPoint int
set @StartPoint = datediff(ss, '01/01/1970', getutcdate())-3800
SELECT InteractionTrack.workgroupid, Sum(InteractionTrack.answer) AS HandledCalls,
sum(queueduration)/Sum(InteractionTrack.answer) as AvgQueTime
FROM dbo.interactiontrack
WHERE
enddate > 1122392945
and
enddate
BETWEEN
(CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) - 1800
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) END )
AND
CASE
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 < 1801 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600)
WHEN datediff(ss, '01/01/1970', getutcdate()) % 3600 > 1800 THEN
datediff(ss, '01/01/1970', getutcdate()) - (datediff(ss, '01/01/1970', getutcdate()) % 3600) + 1800 END
and workgroupid >0
group by workgroupid