このselectステートメントを使用して、異なるテーブルから情報を分離し、同じ構造を維持し、最後に結果をグループ化しますが、 "fi"テーブルには何千もの行があるため、結果を処理するのに時間がかかります。より高速な結果を得るために、以下のクエリを最適化する方法はありますか?データ型varchar型のft.fdataはSelectステートメントが処理に時間がかかります
select Referencia,Designacao,cor,Tamanho,sum(qtd),entreguesede,stock
from
(
Select
isnull(ST.REF,'') as Referencia
, fi.design as Designacao
, fi.cor Cor
, fi.tam Tamanho
, sum(case when ft.tipodoc=3 then -fi.qtt else fi.qtt end) as Qtd
, isnull((select sum(case when sl.cm=1 then sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) EntregueSede
, isnull((select sum(case when sl.cm<50 then sl.qtt when sl.cm>50 then -sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) Stock
from fi (nolock)
inner join ft (nolock) on ft.ftstamp=fi.ftstamp
inner join td (nolock) on ft.ndoc=td.ndoc and td.regrd=0
left join st (nolock) on fi.ref = st.ref or fi.oref = st.ref
left join sz (nolock) on sz.no = fi.armazem
where fi.composto=0
and ft.fdata between '20160301' and '20160331'
and ft.anulado=0
and ft.fno>=0
and fi.qtt<>0
and ft.tipodoc in(1, 2, 3)
and ((FI.ARMAZEM between 1000 and 1999) or fi.armazem in(10,20))
and isnull(sz.nome,'') like '%' + + '%'
group by FI.REF, fi.design,fi.cor,fi.tam,st.ref
union all
Select
isnull(ST.REF,'') Referencia
, fi.design Designacao
, fi.cor Cor
, fi.tam Tamanho
,sum(case when ft.tipodoc=3 then -fi.qtt else fi.qtt end) as Qtd
, isnull((select sum(case when sl.cm=1 then sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) EntregueSede
, isnull((select sum(case when sl.cm<50 then sl.qtt when sl.cm>50 then -sl.qtt else 0 end) from sl (nolock) where sl.ref=fi.ref and sl.cor=fi.cor and sl.tam=fi.tam and sl.datalc<='20160331'),0) Stock
from fi (nolock)
inner join ft (nolock) on ft.ftstamp=fi.ftstamp
inner join td (nolock) on ft.ndoc=td.ndoc and td.regrd=0
left join st (nolock) on fi.ref = st.ref or fi.oref = st.ref
left join sz (nolock) on sz.no = fi.armazem
where fi.composto=0
and ft.fdata >= '20150505'
and ft.fdata between '20160301' and '20160331'
and ft.anulado=0
and ft.fno>=0
and fi.qtt<>0
and ft.ndoc in (401,501)
and isnull(sz.nome,'') like '%' + + '%'
group by FI.REF, fi.design, fi.cor, fi.tam, st.ref
union all
select
isnull(ST.REF,'') Referencia
, bi.design as Designacao
, bi.cor Cor
, bi.tam Tamanho
,sum(bi.qtt) as Qtd
, isnull((select sum(case when sl.cm=1 then sl.qtt else 0 end) from sl (nolock) where sl.ref=bi.ref and sl.cor=bi.cor and sl.tam=bi.tam and sl.datalc<='20160331'),0) EntregueSede
, isnull((select sum(case when sl.cm<50 then sl.qtt when sl.cm>50 then -sl.qtt else 0 end) from sl (nolock) where sl.ref=bi.ref and sl.cor=bi.cor and sl.tam=bi.tam and sl.datalc<='20160331'),0) Stock
from bi(nolock)
inner join bo(nolock) on bo.bostamp = bi.bostamp
inner join bo2(nolock) on bo2stamp = bo.bostamp
inner join ts(nolock) on ts.ndos = bo.ndos and ts.ndos = 60
left join st(nolock) on bi.ref = st.ref
left join sz(nolock) on sz.no = bi.armazem
where bi.composto=0
and bo.dataobra between '20160301' and '20160331'
and bo2.anulado= 0
and bo.obrano >= 0
and bi.qtt<>0
and isnull(sz.nome,'') like '%' + + '%'
group by bi.REF, bi.design, bi.cor, bi.tam, st.ref
) a
group by Referencia,Designacao,cor,Tamanho,entreguesede,stock
order by 1,2,3,4,6,7
あなたが実行計画のXMLを取得する方法を知っていますか?もしそうなら、投稿してください。そうでない場合は、次のURLを参照してください。https://msdn.microsoft.com/en-us/library/ms190646.aspx –
何千もの行で問題は発生しません。適切なインデックスがあることを確認しましたか?あなたがSQLサーバを使用している場合は、クエリアナライザを見てください。 – DoctorMick
「20160301」と「20160331」の間の「ft.fdata」は、その月のすべてではないことにご注意ください。 – shadow