Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Slow Query for Unknown Reason

Posted on 2011-09-12
10
Medium Priority
?
326 Views
Last Modified: 2012-05-12
I'm trying to execute the following query but I can't figure out why it takes about 30 seconds to run:
SELECT CONVERT(varchar(10), DATEADD(D, [Number], '09/12/2011'), 101) As [TheDate],
 (SELECT COUNT([PatientID]) FROM [Patients] WHERE [NextExam] <= DATEADD(D, [Number], '09/12/2011')) AS [NextCount],
 (SELECT COUNT([PatientID]) FROM [Patients] WHERE [POEExam] <= DATEADD(D, [Number], '09/12/2011')) AS [POECount]
FROM [Numbers] WHERE [Number] < DATEDIFF(D, '09/12/2011', DATEADD(M, 2, '09/12/2011')) ORDER BY [TheDate];

Open in new window

All I'm trying to do is iterate thru two months of data and collecting the number of patient exams that are due by the date in question.  The actual date ('09/12/2011') is the only value that is dynamically inputted at run-time.

The [Numbers] table has a primary key [Number] column that is an integer with values from 0 to 365.

If I replace the
<= DATEADD(D, [Number], '09/12/2011')
with
<= DATEADD(D, <any number>, '09/12/2011')
the query finishes in a fraction of a second.

It also does not seem to matter if I iterate thru a month or an entire year, it still takes about 30 seconds.

If anyone has any thoughts on how I can optimize or rewrite the query to execute faster I would greatly appreciate it.
0
Comment
Question by:Kavalleri
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36527282
Well. ORDER BY is always slow down your query. Try to remove order by. If still you want order by then create a TABLE variable and place the selected records. Then apply ORDER BY on the Table variable.
0
 
LVL 8

Expert Comment

by:Ghunaima
ID: 36527325
Issuing two SELECTs on Patients table is another case for slow speed , try attached code it will reduce the time a bit

 
select thedate, sum([nextcount]) NextCount, sum([POECount]) [POECount] from
(SELECT CONVERT(varchar(10), DATEADD(D, [Numbers].[Number], '09/12/2011'), 101) As [TheDate],
 case when [Patients].[NextExam] <= DATEADD(D, [Patients].[Number], '09/12/2011') then 1 else 0 end AS [NextCount],
 case when [Patients].[POExam] <= DATEADD(D, [Patients].[Number], '09/12/2011') then 1 else 0 end AS [POECount]
FROM [Numbers], [Patients] WHERE [Numbers].[Number] < DATEDIFF(D, '09/12/2011', DATEADD(M, 2, '09/12/2011')) 
) x
group by thedate
ORDER BY [TheDate]

Open in new window

0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 36527336
the better way in my experience for good performance  is

a) create a view for often used queries or
b) create stored procedures which accepts parameters as input and sends the results back as output. there should be a difference in performance.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 1

Author Comment

by:Kavalleri
ID: 36527338
I have attempted dropping the ORDER BY clause while I was trying to optimize it myself but it did not run any faster.  Also I discovered all I need to do is replace the [Number] with <any number> in the DATEADD function in the WHERE clause and it finishes in less than a second.  So I'm confused as to why this would make that big a difference.

Thanks.
0
 
LVL 8

Expert Comment

by:Ghunaima
ID: 36527372
What is the structure of the tables? DO you have a column named Number in Patients table or you want to use the Number from Numbers table to be used in your internal queries. If that is the case Try this
select thedate, sum([nextcount]) NextCount, sum([POECount]) [POECount] from
(SELECT CONVERT(varchar(10), DATEADD(D, [Numbers].[Number], '09/12/2011'), 101) As [TheDate],
 case when [Patients].[NextExam] <= DATEADD(D, [Numbers].[Number], '09/12/2011') then 1 else 0 end AS [NextCount],
 case when [Patients].[POExam] <= DATEADD(D, [Numbers].[Number], '09/12/2011') then 1 else 0 end AS [POECount]
FROM [Numbers], [Patients] WHERE [Numbers].[Number] < DATEDIFF(D, '09/12/2011', DATEADD(M, 2, '09/12/2011')) 
) x
group by thedate
ORDER BY [TheDate]

Open in new window

0
 
LVL 1

Author Comment

by:Kavalleri
ID: 36527410
Ghunaima,

The [Numbers].[Number] column is purely to help iterate thru each date between the start and end date in question.  There is no [Number] column in the [Patients] table.

Thanks for the query but it still took about 30 seconds to execute.  I then took the same query and replaced the [Numbers].[Number] in the CASE statement with just any random number and it executed in a fraction of a second.  I just don't know why the optimizer treats those values any different.

Does anyone know a better way to iterate thru a date range if those dates are not specifically in the database?
0
 
LVL 1

Author Comment

by:Kavalleri
ID: 36527422
shasunder:

I have attempted creating a stored procedure for this and surprisingly it did not run any better.  I have not attempted creating a view for this so I will have to give that a try and let you know.

Thanks.
0
 
LVL 39

Expert Comment

by:appari
ID: 36527751
what are you exactly trying to do? explain us with sample data.

data returned by fixing a number and using Number column value in dateadd function will return you different results.

for testing try this
declare @Numbers table(number int)
declare @i int

set @i=0
while @i<365
begin 
insert into @Numbers  select @i
set @i=@i+1
end
--select * from @Numbers  
SELECT 
Number,
DATEDIFF(D, '09/12/2011', DATEADD(M, Number, '09/12/2011')) ,
CONVERT(varchar(10), DATEADD(D, [Number], '09/12/2011'), 101) As [TheDate]
FROM @Numbers   [Numbers] WHERE [Number] < DATEDIFF(D, '09/12/2011', DATEADD(M, Number, '09/12/2011')) 
ORDER BY [TheDate];

Open in new window


replace Number with 2 will return you 61 rows where as using Number returns 364.
0
 
LVL 39

Expert Comment

by:appari
ID: 36527777
also try this

SELECT CONVERT(varchar(10), DATEADD(D, [Number], '09/12/2011'), 101) As [TheDate],
 (SELECT COUNT([PatientID]) FROM [Patients] WHERE [NextExam] <= DATEADD(D, [Number], '09/12/2011')) AS [NextCount],
 (SELECT COUNT([PatientID]) FROM [Patients] WHERE [POEExam] <= DATEADD(D, [Number], '09/12/2011')) AS [POECount]
FROM [Numbers] ORDER BY [TheDate];

Open in new window

0
 
LVL 8

Accepted Solution

by:
Ghunaima earned 1500 total points
ID: 36534520
Replacing Numbers.Number with any fixed no is equivalent to having a single row in Numbers table so the time taken will be less as compared to when Numbers contains more than one row.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question