SQLserver子查询上的性能问题

时间:2015-06-23 16:45:32

标签: sql-server-2008

我正在做一个sql查询以确定哪些文章符合某些条件(不是在4个月内出售,在12个月sol 2或更少的单位出售并且有股票)我尝试使用此查询

select distinct(lineas.codart), articulos.codpro
from lineas
inner join articulos on lineas.codart=articulos.codart
INNER JOIN stock ON stock.codart=lineas.codart
where lineas.codart NOT IN(
    SELECT LINEAS.CODART
    FROM LINEAS
    WHERE DATEDIFF("d",lineas.fecalb,getdate())<120
) or ( lineas.codart in(
        select distinct(codart)
        from lineas
        where DATEDIFF("d",lineas.fecalb,getdate())<365 and DATEDIFF("d",lineas.fecalb,getdate())>120 and lineas.unidad<=2 )
) AND stock.stoexi>0;

但是这并没有给我预期的结果,并且在查询中保持50分钟。

líneas表有650.000行,我认为不正常。

原谅我的英语,谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

你能试试吗?

select distinct(lineas.codart), articulos.codpro 
from lineas o
inner join articulos on lineas.codart=articulos.codart 
INNER JOIN stock ON stock.codart=lineas.codart 
where  NOT exists (
                    SELECT top 1 1 
                    FROM LINEAS i 
                    WHERE (
                                (DATEDIFF("d",i.fecalb,getdate())120 and i.unidad0)
                                )   
                                and o.codart = i.codart
                            )   
                 )