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 中表现不佳。两个环境中的表具有相同的属性(列数据类型和索引)。我猜想选择子句的“+”运算符有一些可能性。谁能帮我 ?

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 的存在可能会更好。+1

于 2012-11-09T14:04:02.570 回答