Следующий запрос выполняется очень быстро без подзапросов ttfj_wtd и ttfj_mtd. Но без того, чтобы добавить обратно, они работают 5 + минут. Любые идеи о том, как его настроить?подзапрос работает очень медленно SQL Server
select eid, tech_id, tech_name, w_start, w_end, m_start, m_end
,(select firstname+' '+lastname from ems_nyc_employee where eid=a.supeid) as SupName
,(select firstname+' '+lastname from ems_nyc_employee where eid=a.mngreid) as MngrName
,activitydate
,shift_start+' - '+shift_end as [shift]
,shift_start_time
,login_time
,first_ip_time
,datediff(mi, shift_start_time, first_ip_time) as ttfj_yest
,(select avg(datediff(mi, shift_start_time, first_ip_time)) from arr_tech a2 where a2.eid=a.eid and a2.activitydate between w_start and w_end) as ttfj_wtd
,(select avg(datediff(mi, shift_start_time, first_ip_time)) from arr_tech a2 where a2.eid=a.eid and a2.activitydate between m_start and m_end) as ttfj_mtd
from
arr_tech a, dates d
where d.rep_date=convert(date, getdate()-1) and a.activitydate=convert(date, getdate()-1)
1) Другие исправленные подзапросы 2) CROSS JOIN Это может быть улучшено, но сначала вы должны подготовить демонстрационный пример http://sqlfiddle.com с образцами данных и нужным набором результатов – lad2025
Нужны некоторые планы выполнения. – dfundako
вы можете попробовать [вручную создавать индексы] (https://msdn.microsoft.com/en-us/library/ms188783.aspx) –