2015-06-23 23 views
0

我是triyin做一個sql查詢來確定哪些文章符合某些條件(在4個月內沒有賣出,在12個月sol 2或更少的單位和有股票)我試着用這個查詢在SQLserver子查詢上的性能問題

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行,我不認爲是正常的。

原諒我的英語,併爲您的幫助感謝

+0

我猜我正確地縮進了。你應該花一些時間來自己格式化 - 不是每個人都有一個10英里寬的顯示器。如果這樣做是正確的,那麼你在混合使用'或'和'和'子句而沒有必要的括號來執行操作符排序,這可能是問題的根源。 –

回答

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 
          ) 
       )     

+0

非常感謝這是多多少少,我需要 – Ion