查询和子查询优化

时间:2019-03-06 15:17:09

标签: sql sql-server

我有这个查询。问题是,大约需要5分钟或更长时间才能被执行。 我正在使用子查询将仅> 200的值带给我

ALTER PROCEDURE [dbo].[RATIO] @DATED nvarchar(30),@DATEF nvarchar(30), @REG nvarchar(30)

SELECT 
[REGION] = ET_REGION,
[MAGASIN] = ET2.ET_LIBELLE ,
(SELECT
    COUNT(DISTINCT GL_NUMERO)

FROM GCLIGNEARTDIM

LEFT OUTER JOIN ETABLISS ET4 ON GL_ETABLISSEMENT=ET4.ET_ETABLISSEMENT

WHERE

CAST(GL_DATEPIECE AS DATE)  BETWEEN CAST(@DATED AS DATE) AND CAST(@DATEF AS DATE)
AND ET4.ET_LIBELLE = ET2.ET_LIBELLE
AND GP_TOTALTTC < 200
)'S : <200'

FROM GCLIGNEARTDIM

LEFT OUTER JOIN ETABLISS ET2 ON GL_ETABLISSEMENT=ET2.ET_ETABLISSEMENT 

WHERE 
CAST(GL_DATEPIECE AS DATE)  BETWEEN CAST(@DATED AS DATE) AND CAST(@DATEF AS DATE)

GROUP BY
ET_REGION,
ET2.ET_LIBELLE

1 个答案:

答案 0 :(得分:0)

在没有基础数据的情况下很难做到这一点,但据我所知您正在做什么,那么类似的事情可能会对您有所帮助;

random(requirement_coverage(100))