Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I write a query the result of the query is fine but issue with the time that is taking to complete the execution. For increase the performance i create index as per my query so this decrease from 5 minutes to 0.44 seconds. But i want to make this more faster. The issue i release with my function that i comment then its execution time is 0.00 but the function i am using is important for me. Is any option to decrease my execution time of my query .

**My function*

ALTER FUNCTION [dbo].[fnCount]

(

      @StartDate DATE,@EndDate DATE,@DropDate DATE

)

RETURNS INT     
AS    
BEGIN    
      DECLARE @WeekStartDate DATE    
      DECLARE @WeekEndDate DATE    
      DECLARE @Count AS INT    
      SET @Count=0    
      SET @WeekStartDate=CONVERT(DATE,DATEADD(dd, -(DATEPART(dw, @DropDate)-1), @DropDate))    
      SET @WeekEndDate=CONVERT(DATE,DATEADD(dd, 7-(DATEPART(dw, @DropDate)), @DropDate))          

      IF @StartDate <= @WeekStartDate    
            SET @StartDate=@WeekStartDate    
      IF @EndDate >=@WeekEndDate    
            SET @EndDate=@WeekEndDate  
      SELECT @Count=COUNT(distinct DROPDATE)    
      FROM  orde_    
      WHERE  orde_.CANCELLED = 0     
      AND DROPDATE >= @StartDate AND DROPDATE <= @EndDate    
      AND (DISPID IS NULL OR DISPID NOT IN ( '291', '327') )    
      RETURN @Count

END

Query

    SELECT READYDATE,TOTAL,DRV1PAY,ORDERNUM,
CASE WHEN dbo.fnCount('20170901','20171031',DROPDATE) >=4
THEN  (CONVERT(VARCHAR(4),LEFT(DROPDATE,4))  + '' + CONVERT(VARCHAR(4),DATEPART( wk, DROPDATE)))
ELSE NULL
END AS WEEKOfYear1,
FROM  orde_
WHERE  CANCELLED = 0  AND DROPDATE >='20170901'  AND DROPDATE <='20171031'
AND (DISPID IS NULL OR DISPID NOT IN ( '291', '327') )
ORDER BY DISPID

Thanks for your help and comments

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
254 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...