Hi
I have this query that works fine when tested against Pre-Production data, but when i took it to Production it took forever to execute
below is the query
--Start
DECLARE @Agent VARCHAR(25),
@Queue VARCHAR(10),
@Status VARCHAR(15),
@Reason VARCHAR(25),
@Time DATETIME,
@Date DATE,
@TimeStamp BIGINT,
@TotalTime BIGINT,
@Version VARCHAR(5),
@EndTime DATETIME,
@NAgent VARCHAR(25),
@NStartTime DATETIME,
@NEndTime DATETIME,
@NDate DATE,
@NCondition VARCHAR(15),
@NActEvent VARCHAR(25),
@DateFrom VARCHAR(10),
@DateTo VARCHAR(10)
SET @DateFrom = CAST(GETDATE() - 1 AS DATE)
SET @DateTo = CAST(GETDATE() - 1 AS DATE)
--Inserting dayly from AgentTimeDetail
--INSERT INTO AgentTimeDetailNonAdOccurance
SELECT Agent, Queue, Status, Reason, Time, Date, TimeStamp, TotalTime, version, EndTime, 'No' Non_Adherence_Occurance
FROM AgentTimeDetail
WHERE Date BETWEEN @DateFrom AND @DateTo
--An Outer Cursor too loop through Non Adherence Data
DECLARE NonAdherenceCursor CURSOR
FOR
SELECT CREATED, AGENTID, DATEADD(S, DURATION, CREATED), CAST(DAY_TIMESTAMP AS DATE)
FROM AgentAdherence
WHERE CAST(DAY_TIMESTAMP AS DATE) BETWEEN @DateFrom AND @DateTo
--AND AGENTID = 'test-s1032339'
OPEN NonAdherenceCursor
FETCH NEXT FROM NonAdherenceCursor INTO @NStartTime, @NAgent, @NEndTime, @NDate
WHILE @@FETCH_STATUS = 0
BEGIN
--Inner Cursor to loop through the Agent Data
DECLARE AgentTimeDetailCursor CURSOR
FOR
SELECT Agent, Queue, Status, Reason, Time, Date, Timestamp, TotalTime, version, EndTime
FROM AgentTimeDetail
WHERE Date BETWEEN @DateFrom AND @DateTo
--AND Agent = 'test-s1032339'
OPEN AgentTimeDetailCursor
FETCH NEXT FROM AgentTimeDetailCursor INTO @Agent, @Queue, @Status, @Reason, @Time, @Date,
@TimeStamp, @TotalTime, @Version, @EndTime
--Checking for Non Adherence time that bounds agent activities, either partially of Fully
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @NCondition = ''
IF ((@NStartTime < @Time) AND ((@NEndTime > @Time) AND (@NEndTime < @EndTime)))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Partial',
@NCondition = 'Partial'
WHERE Time = @Time
AND Agent = @NAgent
IF (((@NStartTime > @Time) AND (@NStartTime < @EndTime)) AND (@NEndTime <= @EndTime))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Partial',
@NCondition = 'Partial'
WHERE Time = @Time
AND Agent = @NAgent
IF ((@NStartTime <= @Time) AND (@NEndTime >= @EndTime))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Full',
@NCondition = 'Full'
WHERE Time = @Time
AND Agent = @NAgent
IF (((@NStartTime > @Time) AND (@NStartTime < @EndTime)) AND (@NEndTime > @EndTime))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Partial',
@NCondition = 'Partial'
WHERE Time = @Time
AND Agent = @NAgent
FETCH NEXT FROM AgentTimeDetailCursor INTO @Agent, @Queue, @Status, @Reason, @Time, @Date,
@TimeStamp, @TotalTime, @Version, @EndTime
END
CLOSE AgentTimeDetailCursor
DEALLOCATE AgentTimeDetailCursor
FETCH NEXT FROM NonAdherenceCursor INTO @NStartTime, @NAgent, @NEndTime, @NDate
END
CLOSE NonAdherenceCursor
DEALLOCATE NonAdherenceCursor
----End
Please help with a way i can optimize it
Thank you
I have this query that works fine when tested against Pre-Production data, but when i took it to Production it took forever to execute
below is the query
--Start
DECLARE @Agent VARCHAR(25),
@Queue VARCHAR(10),
@Status VARCHAR(15),
@Reason VARCHAR(25),
@Time DATETIME,
@Date DATE,
@TimeStamp BIGINT,
@TotalTime BIGINT,
@Version VARCHAR(5),
@EndTime DATETIME,
@NAgent VARCHAR(25),
@NStartTime DATETIME,
@NEndTime DATETIME,
@NDate DATE,
@NCondition VARCHAR(15),
@NActEvent VARCHAR(25),
@DateFrom VARCHAR(10),
@DateTo VARCHAR(10)
SET @DateFrom = CAST(GETDATE() - 1 AS DATE)
SET @DateTo = CAST(GETDATE() - 1 AS DATE)
--Inserting dayly from AgentTimeDetail
--INSERT INTO AgentTimeDetailNonAdOccurance
SELECT Agent, Queue, Status, Reason, Time, Date, TimeStamp, TotalTime, version, EndTime, 'No' Non_Adherence_Occurance
FROM AgentTimeDetail
WHERE Date BETWEEN @DateFrom AND @DateTo
--An Outer Cursor too loop through Non Adherence Data
DECLARE NonAdherenceCursor CURSOR
FOR
SELECT CREATED, AGENTID, DATEADD(S, DURATION, CREATED), CAST(DAY_TIMESTAMP AS DATE)
FROM AgentAdherence
WHERE CAST(DAY_TIMESTAMP AS DATE) BETWEEN @DateFrom AND @DateTo
--AND AGENTID = 'test-s1032339'
OPEN NonAdherenceCursor
FETCH NEXT FROM NonAdherenceCursor INTO @NStartTime, @NAgent, @NEndTime, @NDate
WHILE @@FETCH_STATUS = 0
BEGIN
--Inner Cursor to loop through the Agent Data
DECLARE AgentTimeDetailCursor CURSOR
FOR
SELECT Agent, Queue, Status, Reason, Time, Date, Timestamp, TotalTime, version, EndTime
FROM AgentTimeDetail
WHERE Date BETWEEN @DateFrom AND @DateTo
--AND Agent = 'test-s1032339'
OPEN AgentTimeDetailCursor
FETCH NEXT FROM AgentTimeDetailCursor INTO @Agent, @Queue, @Status, @Reason, @Time, @Date,
@TimeStamp, @TotalTime, @Version, @EndTime
--Checking for Non Adherence time that bounds agent activities, either partially of Fully
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @NCondition = ''
IF ((@NStartTime < @Time) AND ((@NEndTime > @Time) AND (@NEndTime < @EndTime)))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Partial',
@NCondition = 'Partial'
WHERE Time = @Time
AND Agent = @NAgent
IF (((@NStartTime > @Time) AND (@NStartTime < @EndTime)) AND (@NEndTime <= @EndTime))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Partial',
@NCondition = 'Partial'
WHERE Time = @Time
AND Agent = @NAgent
IF ((@NStartTime <= @Time) AND (@NEndTime >= @EndTime))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Full',
@NCondition = 'Full'
WHERE Time = @Time
AND Agent = @NAgent
IF (((@NStartTime > @Time) AND (@NStartTime < @EndTime)) AND (@NEndTime > @EndTime))
UPDATE AgentTimeDetailNonAdOccurance
SET Non_Adherence_Occurance = 'Partial',
@NCondition = 'Partial'
WHERE Time = @Time
AND Agent = @NAgent
FETCH NEXT FROM AgentTimeDetailCursor INTO @Agent, @Queue, @Status, @Reason, @Time, @Date,
@TimeStamp, @TotalTime, @Version, @EndTime
END
CLOSE AgentTimeDetailCursor
DEALLOCATE AgentTimeDetailCursor
FETCH NEXT FROM NonAdherenceCursor INTO @NStartTime, @NAgent, @NEndTime, @NDate
END
CLOSE NonAdherenceCursor
DEALLOCATE NonAdherenceCursor
----End
Please help with a way i can optimize it
Thank you