I need help optimizing this query. I know there are many table joins, but unfortunately, we need the data from these tables. If it's not smart to join this many tables, what alternatives do I have?
What can I do to make this run faster. Right now, it's slow as a slug returning any results. I tried using a temp table hoping to speed things up, but that didn't work for me. Thanks for your help ahead of time.
regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
What can I do to make this run faster. Right now, it's slow as a slug returning any results. I tried using a temp table hoping to speed things up, but that didn't work for me. Thanks for your help ahead of time.
Code:
select j.JobID,
j.JobNumber,
j.ClientID,
g.GroupName as 'CompanyName',
f.FacilityID,
f.FacilityName,
j.UserID,
u_pm.FirstName + ' ' + u_pm.LastName as 'ProjectManager',
j.ProjectName,
(select min(dbo.fn__FormatDate(b.TimeEventStart,'mm/dd/yyyy'))
from ems_test2..tblBooking b
where ReservationId = j.ReservationId) as 'MinStudyDate',
m.Moderator,
j.Color,
j.ReservationID,
r.EventName,
s.Description as 'EMSStatus',
j.Notes,
j.SpecialNeeds,
j.CoopAdvancedRecd,
j.ContractSigned,
c.ID as 'ContactId',
c.Contact,
c.Phone as 'ContactPhone',
(select max(dbo.fn__FormatDate(b.TimeEventStart,'mm/dd/yyyy'))
from ems_test2..tblBooking b
where ReservationId = j.ReservationId) as 'StudyDate'
into #tblJobList3
from tblJobs j,
ems_test2..tblGroup g,
tblFacility f,
tblUsers u_pm,
tblModerator m,
ems_test2..tblReservation r,
ems_test2..tblContact c,
tblGlobalRelations gbl,
tblBids b,
ems_test2..tblStatus s
where j.ClientId = g.ID
and j.FacilityID = f.FacilityID
and j.UserID = u_pm.UserId
and j.Moderator *= m.ModeratorId
and g.ID = c.GroupID
and j.JobId = gbl.JobId
and b.BidId = gbl.BidId
and b.ClientContactId = c.ID
and j.ReservationId = r.ID
and j.EMSStatus = s.ID
and j.EMSStatus not in (5,6,12)
and gbl.NetFacBidId is null
and j.FacilityID = @FacilityId
and j.ActiveFlag = 1
order by StudyDate desc
select * from #tblJobList3 where cast(convert(varchar(20),StudyDate,101) as datetime) >= cast(convert(varchar(20),getdate(),101) as datetime) --order by StudyDate desc
drop table #tblJobList3
regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.