2016-04-19 19 views
0

次のクエリは、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

1)より多くのコア化されたサブクエリ2)CROSS JOIN改善される可能性がありますが、まずサンプルデータと目的の結果セットを使用してhttp://sqlfiddle.comデモを用意してください。 – lad2025

+0

実行計画が必要です。 – dfundako

+0

[手動でインデックスを作成する](https://msdn.microsoft.com/en-us/library/ms188783.aspx)を試してみてください。 –

答えて

0

これは、読みやすくするためにほんの少しアップ固定コードです:arr_tech(rep_date)dates(rep_date)

select eid, tech_id, tech_name, w_start, w_end, m_start, m_end, 
     (select firstname+' '+lastname 
     from ems_nyc_employee e 
     where e.eid = a.supeid 
     ) as SupName, 
     (select firstname+' '+lastname 
     from ems_nyc_employee e 
     where e.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 join 
    dates d 
    on a.activitydate = d.rep_date 
where a.activitydate = convert(date, getdate() - 1); 

まず、あなたは、外部クエリのインデックスをしたいです。

次に、サブクエリのインデックス:ems_nyc_employee(eid, firstname, lastname)arr_tech(eid, m_start, m_end)が必要です。

これらのインデックスが機能しない場合は、クエリを書き直す必要があります。しかし、これで十分かもしれません。フォーマットの

とNotes:FROM句の

  • 決して使用コンマ; 常に明示的なJOIN構文を使用してください。
  • すべての列名を修飾しますが、特に相関サブクエリの列名を修飾します。相関条件が間違っていて、それを修正しようとすると非常に簡単になるのは本当に簡単です。
  • クエリを再度読み込んで理解する必要があるかのように、クエリの書式を設定してください。
関連する問題