0
       `select * from DETAIL a
        where a.BUILD > GETDATE() - 90 s
        and (a.IN + a.Rt) NOT IN (SELECT Sample_IN + Rt FROM  SUMMARY)                  
        and (a.Rt + a.Err) IN 
       (SELECT Rt + Err 
        FROM SUMMARY 
        where (sample_in + rt + err) NOT IN 
       (SELECT in + rt + err FROM DETAIL)) 
        group by a.rt, a.plant, a.in, a.build`

このクエリはパフォーマンスの問題を示しています。sql2000 サーバーではより高速に実行されますが、sql2008R2 ではパフォーマンスが低下します。両方の環境のテーブルには同じプロパティ (列のデータ型とインデックス) があります。select句の「+」演算子にはある程度の確率があると思います。誰でも私を助けることができますか?

4

2 に答える 2

3

フィールドを連結すると、インデックスは機能しません。これらのフィールドをすでに結合している列をテーブルに作成し、それらのフィールドにインデックスを作成できます。これにより、パフォーマンスが向上します。

また、このクエリはより高速に実行され、現在のインデックスを使用することに注意してください(タイプミスはご容赦ください。テーブル定義は含まれていません):

select * 
  from DETAIL a
 where a.BUILD > DateAdd( Day, -90, GetDate() )
   and not exists ( select null 
                      from SUMMARY 
                     where SUMMARY.Sample_IN = a.IN and SUMMARY.Rt  = a.Rt )
   and exists ( select null
                 from SUMMARY 
                where not exists ( select null
                                     from DETAIL 
                                    where DETAIL.in = SUMMARY.Sample_IN 
                                      and DETAIL.Rt = SUMMARY.Rt 
                                      and DETAIL.Err = SUMMARY.Err)
                  and a.Rt = SUMMARY.Rt
                  and a.Err = SUMMARY.Err ) 
group by a.rt, a.plant, a.in, a.build
于 2012-11-09T13:44:42.063 に答える
0
    select * from DETAIL a 
    left outer join SUMMARY sm1
      on a.IN = sm1.Sample_IN 
     and a.Rt = sm1.Rt 
    join SUMMARY sm2
      on a.Rt = sm2.Rt
     and a.Err = sm2.Err
    left outer join Detail d 
      on sm2.Sample_IN = d.in
     and sm2.rt = d.rt
     and sm2.err = d.err
    where a.BUILD > GETDATE() - 90 s
    and sm1.Rt is null 
    and d.in is null
    group by a.rt, a.plant, a.in, a.build

これは結合を使用しています。あなたの問題については、 Dominic Goulet からの exists がおそらくうまくいくでしょう。+1

于 2012-11-09T14:04:02.570 に答える