I have a query that is taking very long to execute, literally takes more than an hour or two, which is not very productive.
I have a primary key on my main table and a foreign key on all LEFT OUTER JOIN tables.
Not quite sure how else to improve the execution time?
Any suggestions what I should be doing?
truncate table MyDay_Ward insert into MyDay_Ward (location, unitno, [patient name], sex, age, location_description, [bed space], EpiStart, [episode los], [spell start] , TTO, AsAtDate, Clinic, AppointmentDate, theatre, OperationDate, [Into Ana], [Into Theatre], Duration, [Into Recovery] , APPOINTMENT, PROCEDURE1, APPOINTMENT_Lung, app_dttm, IDA, [Type], request_time, Status1, [Desc], request_time_ima , [Status], Desc1, [Exam name], CRIS_Date, Endoscopy, EndoscopyDate, PlannedDischarge, appointmentdate1, appointmentType , CommonName, [exam name1], event_dttm) select w.location , w.unitno , w.[patient name] , w.sex , w.age , w.location_description , w.[bed space] , w.EpiStart , w.[episode los] , w.[spell start] , w.TTO , w.AsAtDate , l.Clinic , l.AppointmentDate , t.theatre , t.OperationDate , t.[Into Ana] , t.[Into Theatre] , t.Duration , t.[Into Recovery] , e.APPOINTMENT , e.PROCEDURE1 , lu.APPOINTMENT_Lung , m.app_dttm , m.IDA , c.[Type] , tp.request_time , tp.[Status] Status1 , tp.[Desc] , i.request_time_ima , i.[Status] , i.[Desc] Desc1 , cr.[Exam name] , cr.CRIS_Date , en.Endoscopy , en.EndoscopyDate , pd.PlannedDischarge , ci.appointmentdate appointmentdate1 , ci.appointmentType , ci.CommonName , cd.[exam name] [exam name1] , cd.event_dttm from MyDay_Ward_Interim w left outer join MyDay_Lorenzo l on left(w.unitno,7) = left(l.patientid,7) left outer join MyDay_Theatres t on left(w.unitno,7) = left(t.unitno,7) left outer join MyDay_Echo e on left(w.unitno,7) = left(e.hospital_number,7) collate database_default left outer join MyDay_Lung lu on left(w.unitno,7) = left(lu.hospital_number,7) collate database_default left outer join MyDay_Mosaiq m on left(w.unitno,7) = left(m.ida,7) left outer join MyDay_Chaplaincy c on left(w.unitno,7) = left(c.identifier,7) collate database_default left outer join MyDay_Therapies_PICS tp on left(w.unitno,7) = left(tp.reg,7) collate database_default left outer join MyDay_Imaging_PICS i on left(w.unitno,7) = left(i.reg,7) collate database_default left outer join MyDay_CRIS cr on left(w.unitno,7) = left(cr.[best hosp no],7) left outer join MyDay_Endoscopy en on left(w.unitno,7) = left(en.unitno,7) left outer join MyDay_PICS_Discharge pd on left(w.unitno,7) = left(pd.unitno,7) left outer join MyDay_CIAO ci on left(w.unitno,7) = left(ci.unitno,7) left outer join MyDay_CRIS_Done cd on left(w.unitno,7) = left(cd.unitno,7) group by w.location , w.unitno , w.[patient name] , w.sex , w.age , w.location_description , w.[bed space] , w.EpiStart , w.[episode los] , w.[spell start] , w.TTO , w.AsAtDate , l.Clinic , l.AppointmentDate , t.theatre , t.OperationDate , t.[Into Ana] , t.[Into Theatre] , t.Duration , t.[Into Recovery] , e.APPOINTMENT , e.PROCEDURE1 , lu.APPOINTMENT_Lung , m.app_dttm , m.IDA , c.[Type] , tp.request_time , tp.[Status] , tp.[Desc] , i.request_time_ima , i.[Status] , i.[Desc] , cr.[Exam name] , cr.CRIS_Date , en.Endoscopy , en.EndoscopyDate , pd.PlannedDischarge , ci.appointmentdate , ci.appointmentType , ci.CommonName , cd.[exam name] , cd.event_dttm