Below piece of code, INNER Join is used 2 times and its costing High on execution plan andtakes more than 2 min to complete. is there a way to reduce the inner joins to speed up the store proc.. Please advice. SET NOCOUNT ON declare @startInSysTime datetime, @endInSysTime datetime SELECT @startInSysTime=dbo.fnSiteTimeToSystemTime(@startDate), @endInSysTime=dbo.fnSiteTimeToSystemTime(@endDate) declare @userIDTable AS Table (Item int) INSERT INTO @userIDTable SELECT ContactUserID FROM ContactUser Where TeamID=@teamID declare @currentDateTime datetime set @currentDateTime=getDate() DECLARE @AuditDays Table(AuditDayID INT) INSERT INTO @AuditDays SELECT AuditDayID FROM AuditDay ad WHERE ad.Date >= @startInSysTime AND ad.Date < @endInSysTime declare @summary as Table (AgentID nvarchar(40), MessageType nvarchar(1), Answered int, [Out] int, OutCust int, Overflow int, Escalated int, AvgGradeOfService int, qaApproved int, goneIntoQA int, Transfered int) INSERT INTO @summary SELECT cu.UserID as 'Agent', cg.MessageType, SUM(case when mat.AuditCodeName='Message Accepted' then 1 else 0 end) as 'Answered', SUM(case when mat.AuditCodeName='New Outbound Message' then 1 else 0 end) as 'Out', Count(distinct case when mat.AuditCodeName='New Outbound Message' then m.CustomerID else NULL end ) as 'OutCust', SUM(case when mat.AuditCodeName='Message Escalated' then 1 else 0 end) as 'Overflow', SUM(case when mat.AuditCodeName='Message Escalated To Skill' then 1 else 0 end) as 'Escalated', AVG(ims.GradeOfService) AS 'AvgGradeOfService', SUM(case when (m.MessageDirection='o' and mat2.auditcodename='Message Not Edited') then 1 else 0 end) as 'qaApproved', SUM(case when (m.MessageDirection='o' and mat2.auditcodename in('Message Deleted', 'Message Edited','Message Not Edited','Message Released')) then 1 else 0 end) as 'goneIntoQA', SUM(case when mat3.AuditCodeName='Message.Transferred' then 1 else 0 end) as 'Transfered' FROM @userIDTable u inner join ContactUser cu on cu.ContactUserID=u.Item inner join MessageAuditTimes mat on (mat.ContactUserID=cu.ContactUserID ) inner join @AuditDays ad on ad.AuditDayID = mat.AuditDayID inner join AuditCode A on A.AuditCodeID = mat.auditcodeid AND A.auditcodename IN ('Message Accepted', 'Message Escalated', 'New Outbound Message', 'Message Escalated To Skill' ) inner join Message m on m.MessageID=mat.MessageID inner join ChannelConfig cg on cg.MessageType = m.MessageType left join MessageAuditTimes mat2 on (m.MessageID=mat2.MessageID AND mat2.auditcodename in('Message Deleted', 'Message Edited','Message Not Edited','Message Passed','Message Released')) left join MessageAuditTimes mat3 on (m.MessageID=mat3.MessageID AND mat3.auditcodename='Message.Transferred') left join InboundMessageStats ims on ims.MessageID = mat.MessageID GROUP BY cu.UserID, cg.ChannelConfigID, cg.MessageType ORDER BY cu.UserID, cg.ChannelConfigID insert into @summary SELECT null, null, SUM(case when mat.AuditCodeName='Message Accepted' then 1 else 0 end) as 'Answered', SUM(case when mat.AuditCodeName='New Outbound Message' then 1 else 0 end) as 'Out', Count(distinct case when mat.AuditCodeName='New Outbound Message' then m.CustomerID else NULL end ) as 'OutCust', SUM(case when mat.AuditCodeName='Message Escalated' then 1 else 0 end) as 'Overflow', SUM(case when mat.AuditCodeName='Message Escalated To Skill' then 1 else 0 end) as 'Escalated', AVG(ims.GradeOfService) AS 'AvgGradeOfService', SUM(case when (m.MessageDirection='o' and mat2.auditcodename='Message Not Edited') then 1 else 0 end) as 'qaApproved', SUM(case when (m.MessageDirection='o' and mat2.auditcodename in('Message Deleted', 'Message Edited','Message Not Edited','Message Released')) then 1 else 0 end) as 'goneIntoQA', SUM(case when mat3.AuditCodeName='Message.Transferred' then 1 else 0 end) as 'Transfered' FROM @userIDTable u inner join ContactUser cu on cu.ContactUserID=u.Item inner join MessageAuditTimes mat on (mat.ContactUserID=cu.ContactUserID ) inner join @AuditDays ad on ad.AuditDayID = mat.AuditDayID inner join AuditCode A on A.AuditCodeID = mat.auditcodeid AND A.auditcodename IN ('Message Accepted', 'Message Escalated', 'New Outbound Message', 'Message Escalated To Skill' ) inner join Message m on m.MessageID=mat.MessageID inner join ChannelConfig cg on cg.MessageType = m.MessageType left join MessageAuditTimes mat2 on (m.MessageID=mat2.MessageID AND mat2.auditcodename in('Message Deleted', 'Message Edited','Message Not Edited','Message Passed','Message Released')) left join MessageAuditTimes mat3 on (m.MessageID=mat3.MessageID AND mat3.auditcodename='Message.Transferred') left join InboundMessageStats ims on ims.MessageID = mat.MessageID SELECT * FROM @summary SET NOCOUNT OFF
Naveen| Press Yes if the post is useful.