473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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?

Jul 23 '05 #1
2 4931
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.

Jul 23 '05 #2
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!

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DFS | last post by:
This UNION query is very slow. With only 3,000 records in the Parent table and 7,000 records in the Child table, it takes about 60 seconds to run and returns about 2200 records. Any ideas on...
2
by: Benoit Le Goff | last post by:
Hello. I test some query on sql server 2000 (sp2 on OS windows 2000) and i want to know why a simple query like this : select * from Table Where Column like '%value' is more slow on 2000 than...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
5
by: garydevstore | last post by:
Hi, I have a table defined as CREATE TABLE ( IDENTITY (1, 1) NOT NULL , NULL , NULL , NOT NULL , NULL , (255) COLLATE SQL_Latin1_General_CP1_CS_AS
2
by: Yonatan Goraly | last post by:
I am in the process of adding PostgreSQL support for an application, in addition to Oracle and MS SQL. I am using PostgreSQL version 7.3.2, Red Hat 9.0 on Intel Pentium III board. I have a...
29
by: wizofaus | last post by:
I previously posted about a problem where it seemed that changing the case of the word "BY" in a SELECT query was causing it to run much much faster. Now I've hit the same thing again, where...
12
by: grace | last post by:
i am wondering why my database retrieval becomes too slow...we set up a new server (ubuntu, breezy badger) machine where we transferred all our files from the old server.. Our new server uses Asus...
2
by: Rowan | last post by:
For some reason when I do a fetchall on a PDO query the array returned has duplicated keys. see below. SELECT name_first, name_last, personal_street, personal_phone_home, personal_phone_cell...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.