减少查询的执行时间

时间:2018-01-11 09:54:07

标签: sql-server-2008 tsql

我写了一个查询,查询的结果很好,但问题是完成执行所花费的时间。为了提高性能,我根据我的查询创建了索引,因此从5分钟减少到0.44秒。但我想让这更快。这个问题我发布了我的功能,我评论然后它的执行时间是0.00但我正在使用的功能对我很重要。是否可以减少查询的执行时间。

**我的功能*

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

查询

    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

感谢您的帮助和意见

0 个答案:

没有答案