Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Optimize queries

$
0
0

I have the following function

CREATEFUNCTION[dbo].[SuiviRupture](@CodeArticle [nvarchar](13),@CodeSite [nvarchar](5),@CodeStructure [nvarchar](13))
RETURNS @calcul TABLE(CAMOY float, QTEMOY float)ASBEGINwith temp as(select
            t1.[datecol], t1.[Prix de vente TTC],
            t1.Quantitéfrom[V_VentePromo] t1innerjoin(selectdistinct[datecol],[Code Article],[Code Structure],[Code Site],
                 row_number()over(Partitionby[Code Article],[Code Structure],[Code Site]orderby[datecol]desc)as rnfrom(selectdistinct[datecol],[Code Article],[Code Structure],[Code Site]from[V_VentePromo] t2where[Code Article]=@CodeArticleand[Code Site]=@CodeSiteand[Code Structure]=@CodeStructure) g) a on a.datecol = t1.datecoland t1.[Code Article]= a.[Code Article]and t1.[Code Structure]= a.[Code Structure]and t1.[Code Site]= a.[Code Site]where
            t1.[Code Article]=@CodeArticleand t1.[Code Site]=@CodeSiteand t1.[Code Structure]=@CodeStructureand rn <=28)INSERT@calculSELECTCASEWHEN COUNT(distinct[datecol])=0THEN0ELSE SUM(convert(float, Quantité))/ count(distinct[datecol])ENDas QTEMOY,CASEWHEN COUNT(distinct[datecol])=0THEN0ELSE SUM(convert(float,[Prix de vente TTC]))/ count(distinct[datecol])ENDAS CAMOYFROM
            tempRETURN;END;

I call this function by this query

select
    t1.[Code Article], t1.[Code Site], t1.[Code Structure],
    u.QTEMOY, u.CAMOYfrom
    V_distinctVente t1crossapply
    dbo.[SuiviRupture](t1.[Code Article], t1.[Code Site], t1.[Code Structure]) u

enter image description here

The execution time is too long, I have a number of rows of V_distinctVente about 10 000 000. How to optimize it?


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>