Unix Technical Forum

Slow SQL Query - Case in Where Stmnt

This is a discussion on Slow SQL Query - Case in Where Stmnt within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, a query of mine slowed down significantly when this statement was added into the where: (DATEDIFF(day, Col_StartDate, GETDATE()) ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:24 AM
shootsie
 
Posts: n/a
Default Slow SQL Query - Case in Where Stmnt

Hi, a query of mine slowed down significantly when this statement was
added into the where:

(DATEDIFF(day, Col_StartDate, GETDATE()) BETWEEN 1 AND


(SELECT CASE datepart(dw, getdate())


WHEN 1 THEN 2


WHEN 2 THEN 3


ELSE 1


END) )

What it is supposed to do is get Friday, Saturday and Sunday's data if
today is Monday -- in addition if the day is Sunday get Friday and
Saturdays data. Otherwise, just get yesterdays data. This works,
however it slowed down the query by 12X. I think it may be the use of
a "case" because if I hard code it there isn't a problem. Any
suggestions for alternatives?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:24 AM
Thomas R. Hummel
 
Posts: n/a
Default Re: Slow SQL Query - Case in Where Stmnt

You didn't mention if there was any indexing on Col_StartDate.
Actaully, you didn't mention much of anything... but you may want to
try moving Col_StartDate out of the DATEDIFF function. Something like
the following:

SET DATEFIRST 7 -- Always explicitly set this when using DATEPART w/
dw

DECLARE @start_date DATETIME, @end_date DATETIME

-- Set end date to midnight this morning
SET @end_date = CAST(CONVERT(CHAR(10), GETDATE(), 112) AS DATETIME)

SET @start_date = CASE DATEPART(dw, GETDATE())
WHEN 1 THEN DATEADD(dd, -2, @end_date)
WHEN 2 THEN DATEADD(dd, -3, @end_date)
ELSE DATEADD(dd, -1, @end_date)
END

SELECT ...
WHERE Col_StartDate BETWEEN @start_date AND @end_date

Since BETWEEN is inclusive, you may need to change it to < and >= the
start and end dates if you have data that has datetime values of
exactly midnight for those days.

HTH,
-Tom.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:24 AM
shootsie
 
Posts: n/a
Default Re: Slow SQL Query - Case in Where Stmnt

Thanks for your help! To be honest, I'm not sure how this speeded it
up, but I put the case statement in a user defined function and that
did the trick!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:06 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com