I have a union query that takes forever about 5 minutes. warning its a union and its long. I can provide more info. but if someone wants to have a look and see if anything is obvious please let me know.
select
tblmembers.memberid,
upper(ltrim(rtrim(tblmembers.firstname))) As FirstName,
upper(rtrim(ltrim(tblmembers.firstname))) + ' ' + upper(ltrim(rtrim(tblmembers.lastname))) As Name,
emailid,
convert(char(12),DOB,1) As BirthDate,
rtrim(cast(DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()), DOB) THEN 0 ELSE 1 END as char)) + ' ' + rtrim(cast (DATEDIFF(month, DATEADD(year, DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()),
DOB) THEN 0 ELSE 1 END ,DOB),getdate()) as char)) as age,
case GoalType when 0 then 'SPRINT' when 1 then 'OLYMPIC' when 2 then 'HALF' when 3 then 'IRONMAN' end As MainGoal,
tblheartrate.heartratemax as MaxAerobic,
weekcredits as PeakCredits,
weekcreditsmaint as MaintCredits,
convert(char(12),lastlogin,1) as LastLogin,
convert(char(12),StartDate,1) as ProgramStart,
convert(char(12),dateadd(week,viewLastProgramDetails.weekspaid,StartDate),1) As PaidTrough,
convert(char(12),GoalDate,1) As GoalRace,
upper(ltrim(rtrim(City))) As City,
upper(ltrim(rtrim(State))) As State,
Country,
upper(rtrim(ltrim(B.firstname))) + ' ' + upper(ltrim(rtrim(B.lastname))) As Coach
from tblmembers inner join viewLastProgramDetails on tblmembers.memberid = viewLastProgramDetails.memberid
left join (select memberid, heartratemax from tblMemberEvents where eventcode = 'RU' and eventsubcode = 'LR' group by memberid,heartratemax) AS tblheartrate
on tblmembers.memberid = tblheartrate.memberid
left join (select memberid,firstname,lastname from tblmembers where IsCoach = 1) AS B on tblmembers.CoachMemberId = B.memberid
Where (viewLastProgramDetails.ProgramType = 0 or viewLastProgramDetails.ProgramType = 2) and (dateadd(day,30,GoalDate) >= getdate() or dateadd(day,30,lastlogin) >= getdate()) and tblmembers.Memberid not in (1,4,5,6,7,11,39,40,193) and tblmembers.lang
= @slang
union
select tblmembers.memberid,
upper(ltrim(rtrim(tblmembers.firstname))) As FirstName,
upper(rtrim(ltrim(tblmembers.firstname))) + ' ' + upper(ltrim(rtrim(tblmembers.lastname))) As Name,
emailid,
convert(char(12),DOB,1) As BirthDate,
rtrim(cast(DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()), DOB) THEN 0 ELSE 1 END as char)) + ' ' + rtrim(cast (DATEDIFF(month, DATEADD(year, DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()),
DOB) THEN 0 ELSE 1 END ,DOB),getdate()) as char)) as age,
'MAINTAIN' as mainGoal,
tblheartrate.heartratemax as MaxAerobic,
weekcredits as PeakCredits,
weekcreditsmaint as MaintCredits,
convert(char(12),lastlogin,1) as LastLogin,
convert(char(12),StartDate,1) as ProgramStart,
convert(char(12),dateadd(week,viewLastProgramDetails.weekspaid,StartDate),1) As PaidTrough,
'' as GoalRace,
upper(ltrim(rtrim(City))) as City,
upper(ltrim(rtrim(State))) as State,
Country,
upper(rtrim(ltrim(B.firstname))) + ' ' + upper(ltrim(rtrim(B.lastname))) As Coach
from tblmembers left join viewLastProgramDetails on tblmembers.memberid = viewLastProgramDetails.memberid
left join (select memberid, heartratemax from tblMemberEvents where eventcode = 'RU' and eventsubcode = 'LR' group by memberid,heartratemax) as tblheartrate
on tblmembers.memberid = tblheartrate.memberid
left join (select memberid,firstname,lastname from tblmembers where IsCoach = 1) AS B on tblmembers.CoachMemberId = B.memberid
Where viewLastProgramDetails.ProgramType = 1 and (dateadd(week,24,StartDate) > getdate()or dateadd(day,30,lastlogin) >= getdate()) and tblmembers.Memberid not in (1,4,5,6,7,11,39,40,193) and tblmembers.lang = @slang
union
select
tblmembers.memberid,
upper(ltrim(rtrim(tblmembers.firstname))) As FirstName,
upper(rtrim(ltrim(tblmembers.firstname))) + ' ' + upper(ltrim(rtrim(tblmembers.lastname))) As Name,
emailid,
convert(char(12),DOB,1) As BirthDate,
rtrim(cast(DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()), DOB) THEN 0 ELSE 1 END as char)) + ' ' + rtrim(cast (DATEDIFF(month, DATEADD(year, DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()),
DOB) THEN 0 ELSE 1 END ,DOB),getdate()) as char)) as age,
'' As MainGoal,
'' as MaxAerobic,
weekcredits as PeakCredits,
weekcreditsmaint as MaintCredits,
convert(char(12),lastlogin,1) as LastLogin,
'' as ProgramStart,
'' As PaidTrough,
'' As GoalRace,
upper(ltrim(rtrim(tblmembers.City))) As City,
upper(ltrim(rtrim(tblmembers.State))) As State,
tblmembers.Country,
upper(rtrim(ltrim(B.firstname))) + ' ' + upper(ltrim(rtrim(B.lastname))) As Coach
from tblmembers left join viewLastProgramDetails on tblmembers.memberid = viewLastProgramDetails.memberid
left join (select memberid,firstname,lastname from tblmembers where IsCoach = 1) AS B on tblmembers.CoachMemberId = B.memberid
where StartDate is Null and dateadd(day,60,lastlogin) >= getdate() and tblmembers.Memberid not in (1,4,5,6,7,11,39,40,193) and tblmembers.lang = @slang
else
select
tblmembers.memberid,
upper(ltrim(rtrim(tblmembers.firstname))) As FirstName,
upper(rtrim(ltrim(tblmembers.firstname))) + ' ' + upper(ltrim(rtrim(tblmembers.lastname))) As Name,
emailid,
convert(char(12),DOB,1) As BirthDate,
rtrim(cast(DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()), DOB) THEN 0 ELSE 1 END as char)) + ' ' + rtrim(cast (DATEDIFF(month, DATEADD(year, DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()),
DOB) THEN 0 ELSE 1 END ,DOB),getdate()) as char)) as age,
case GoalType when 0 then 'SPRINT' when 1 then 'OLYMPIC' when 2 then 'HALF' when 3 then 'IRONMAN' end As MainGoal,
tblheartrate.heartratemax as MaxAerobic,
weekcredits as PeakCredits,
weekcreditsmaint as MaintCredits,
convert(char(12),lastlogin,1) as LastLogin,
convert(char(12),StartDate,1) as ProgramStart,
convert(char(12),dateadd(week,viewLastProgramDetails.weekspaid,StartDate),1) As PaidTrough,
convert(char(12),GoalDate,1) As GoalRace,
upper(ltrim(rtrim(City))) As City,
upper(ltrim(rtrim(State))) As State,
Country,
upper(rtrim(ltrim(B.firstname))) + ' ' + upper(ltrim(rtrim(B.lastname))) As Coach
from tblmembers inner join viewLastProgramDetails on tblmembers.memberid = viewLastProgramDetails.memberid
left join (select memberid, heartratemax from tblMemberEvents where eventcode = 'RU' and eventsubcode = 'LR' group by memberid,heartratemax) AS tblheartrate
on tblmembers.memberid = tblheartrate.memberid
left join (select memberid,firstname,lastname from tblmembers where IsCoach = 1) AS B on tblmembers.CoachMemberId = B.memberid
Where (viewLastProgramDetails.ProgramType = 0 or viewLastProgramDetails.ProgramType = 2) and (dateadd(day,30,GoalDate) >= getdate() or dateadd(day,30,lastlogin) >= getdate()) and tblmembers.Memberid not in (1,4,5,6,7,11,39,40,193) and tblmembers.CoachMemberId
= @coachmemberid
union
select tblmembers.memberid,
upper(ltrim(rtrim(tblmembers.firstname))) As FirstName,upper(rtrim(ltrim(tblmembers.firstname))) + ' ' + upper(ltrim(rtrim(tblmembers.lastname))) As Name,
emailid,
convert(char(12),DOB,1) As BirthDate,
rtrim(cast(DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()), DOB) THEN 0 ELSE 1 END as char)) + ' ' + rtrim(cast (DATEDIFF(month, DATEADD(year, DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()),
DOB) THEN 0 ELSE 1 END ,DOB),getdate()) as char)) as age,
'MAINTAIN' as mainGoal,
tblheartrate.heartratemax as MaxAerobic,
weekcredits as PeakCredits,
weekcreditsmaint as MaintCredits,
convert(char(12),lastlogin,1) as LastLogin,
convert(char(12),StartDate,1) as ProgramStart,
convert(char(12),dateadd(week,viewLastProgramDetails.weekspaid,StartDate),1) As PaidTrough,
'' as GoalRace,
upper(ltrim(rtrim(City))) as City,
upper(ltrim(rtrim(State))) as State,
Country,
upper(rtrim(ltrim(B.firstname))) + ' ' + upper(ltrim(rtrim(B.lastname))) As Coach
from tblmembers left join viewLastProgramDetails on tblmembers.memberid = viewLastProgramDetails.memberid
left join (select memberid, heartratemax from tblMemberEvents where eventcode = 'RU' and eventsubcode = 'LR' group by memberid,heartratemax) as tblheartrate
on tblmembers.memberid = tblheartrate.memberid
left join (select memberid,firstname,lastname from tblmembers where IsCoach = 1) AS B on tblmembers.CoachMemberId = B.memberid
Where viewLastProgramDetails.ProgramType = 1 and (dateadd(week,24,StartDate) > getdate()or dateadd(day,30,lastlogin) >= getdate()) and tblmembers.Memberid not in (1,4,5,6,7,11,39,40,193) and tblmembers.CoachMemberId = @coachmemberid
union
select
tblmembers.memberid,
upper(ltrim(rtrim(tblmembers.firstname))) As FirstName,
upper(rtrim(ltrim(tblmembers.firstname))) + ' ' + upper(ltrim(rtrim(tblmembers.lastname))) As Name,
emailid,
convert(char(12),DOB,1) As BirthDate,
rtrim(cast(DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()), DOB) THEN 0 ELSE 1 END as char)) + ' ' + rtrim(cast (DATEDIFF(month, DATEADD(year, DateDIFF(yy,DOB,getdate())-CASE WHEN getdate()>=DateAdd(yy,DateDIFF(yy,DOB,getdate()),
DOB) THEN 0 ELSE 1 END ,DOB),getdate()) as char)) as age,
'' As MainGoal,
'' as MaxAerobic,
weekcredits as PeakCredits,
weekcreditsmaint as MaintCredits,
convert(char(12),lastlogin,1) as LastLogin,
'' as ProgramStart,
'' As PaidTrough,
'' As GoalRace,
upper(ltrim(rtrim(tblmembers.City))) As City,
upper(ltrim(rtrim(tblmembers.State))) As State,
tblmembers.Country,
upper(rtrim(ltrim(B.firstname))) + ' ' + upper(ltrim(rtrim(B.lastname))) As Coach
from tblmembers left join viewLastProgramDetails on tblmembers.memberid = viewLastProgramDetails.memberid
left join (select memberid,firstname,lastname from tblmembers where IsCoach = 1) AS B on tblmembers.CoachMemberId = B.memberid
Where StartDate is Null and dateadd(day,30,lastlogin) >= getdate() and tblmembers.Memberid not in (1,4,5,6,7,11,39,40,193) and tblmembers.CoachMemberId = @coachmemberid
Luis Vargas