Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL query slow on date comparison?

Author  Topic 

lavenzo
Starting Member

7 Posts

Posted - 2011-12-27 : 01:02:48
hi all..i have a report and it runs very slowww..i found it took very long time when running on this part (inside my report have alot of this kind of query) :-

DECLARE
@startTime datetime,
@endTime datetime,
@campaign int,
@wave int,
@agent varchar(50),
@wrapupdate datetime

SET @startTime = '2011-11-11'
SET @endTime = '2011-11-18'
SET @campaign = 36
set @wave = 0
set @agent = null


select
CM.WRAPUP_DATE ,
Record_Used =  (SELECT COUNT(DISTINCT(CW.SYS_ID)) FROM CH_HISTORY CH JOIN CH_WRAPUP CW ON CH.ID = CW.CH_ID JOIN CM_OPPORTUNITY ON CM_OPPORTUNITY.ID = CW.SYS_ID WHERE
((CW.WRAPUP_DATE BETWEEN(convert(datetime,
cast(year(DATEadd(day,0,CM.WRAPUP_DATE)) as varchar(4)) + '-' +
cast(month(DATEadd(day,0, CM.WRAPUP_DATE))as varchar(2)) + '-' +
cast(day(DATEadd(day,0, CM.WRAPUP_DATE)) as varchar(2))))
AND
(convert(datetime,
cast(year(DATEadd(day,1,CM.WRAPUP_DATE)) as varchar(4)) + '-' +
cast(month(DATEadd(day,1, CM.WRAPUP_DATE))as varchar(2)) + '-' +
cast(day(DATEadd(day,1, CM.WRAPUP_DATE)) as varchar(2))))))
AND
(@campaign is NULL or @campaign = '0' or @campaign =  CM_OPPORTUNITY.CAMPAIGN_ID) AND  (@wave is NULL or @wave = '0' or @wave = CM_OPPORTUNITY.WAVE_ID) AND (@agent is NULL or @agent = '0' or @agent = CH.USER_ID))
FROM CM_OPPORTUNITY AS CM
WHERE
(@startTime is NULL or CM.WRAPUP_DATE >=  @startTime)
AND (@endTime is NULL or CM.WRAPUP_DATE  < @endTime)
AND (@campaign is NULL or @campaign = '0' or @campaign =  CM.CAMPAIGN_ID)
AND (@wave is NULL or @wave = '0' or @wave = CM.WAVE_ID)
AND (@agent is NULL or @agent = '0' or @agent = WRAPUP_USER)
GROUP BY
CM.WRAPUP_DATE
order by CM.WRAPUP_DATE asc


it just a 1200+ records but took me 4min to get the result...anything wrong with my query??

sureshkk
Starting Member

21 Posts

Posted - 2011-12-27 : 01:23:45
Try the following query.
select
CM.WRAPUP_DATE ,
Record_Used = (SELECT COUNT(DISTINCT(CW.SYS_ID)) FROM CH_HISTORY CH JOIN CH_WRAPUP CW ON CH.ID = CW.CH_ID JOIN CM_OPPORTUNITY ON CM_OPPORTUNITY.ID = CW.SYS_ID

WHERE CW.WRAPUP_DATE BETWEEN CONVERT(VARCHAR(10),CM.WRAPUP_DATE,101) AND CONVERT(VARCHAR(10),CM.WRAPUP_DATE+1,101)
AND
(@campaign is NULL or @campaign = '0' or @campaign = CM_OPPORTUNITY.CAMPAIGN_ID) AND (@wave is NULL or @wave = '0' or @wave = CM_OPPORTUNITY.WAVE_ID) AND (@agent is NULL or @agent = '0' or @agent = CH.USER_ID))
FROM CM_OPPORTUNITY AS CM
WHERE
(@startTime is NULL or CM.WRAPUP_DATE >= @startTime)
AND (@endTime is NULL or CM.WRAPUP_DATE < @endTime)
AND (@campaign is NULL or @campaign = '0' or @campaign = CM.CAMPAIGN_ID)
AND (@wave is NULL or @wave = '0' or @wave = CM.WAVE_ID)
AND (@agent is NULL or @agent = '0' or @agent = WRAPUP_USER)
GROUP BY
CM.WRAPUP_DATE
order by CM.WRAPUP_DATE asc
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-27 : 02:09:55
does CM_OPPORTUNITY.WRAPUP_DATE & CH_WRAPUP.WRAPUP_DATE contains time ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

lavenzo
Starting Member

7 Posts

Posted - 2011-12-28 : 00:45:25
@sureshkk: it still slow =(

@khtan: yeah it contains time
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-28 : 00:56:38
the problem is this i feel

http://use-the-index-luke.com/sql/where-clause/obfuscation/smart-logic

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-28 : 00:57:58
quote:
Originally posted by lavenzo


@khtan: yeah it contains time



Both ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

lavenzo
Starting Member

7 Posts

Posted - 2011-12-28 : 01:15:25
@khtan: yeah both contain time

example of the data '2011-11-11 09:40:22.073'
Go to Top of Page
   

- Advertisement -