Solved

Slow Query for Unknown Reason

Posted on 2011-09-12
10
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 500 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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