Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
When i run this script , it takes 39 seconds-
SQL
select distinct PolNum 'Policy/Quote' from table1(nolock) where ndate between DATEADD(month,-1,GETDATE())and GETDATE() 

It fetches only 9163 records

but the below one take only 3 seconds
SQL
select distinct PolNum 'Policy/Quote' from table1(nolock)

it fetches 385416 records

I understand, the where clause is matching all the records with date condition and taking some time there. But i am not satisfied with the answer.

Can anyone, please help me to understand the reason?
Posted
Updated 9-Mar-15 1:20am
v3

Hi,

Its not at all shocking.

Let me give you an example. There are 100 cats. If you have to find total no of cats then obviously it will be a quick response. And if you have to find no of white cat then it will take time to you to find it out.

In the similar way, by putting a where clause you are asking SQL to fetch selected records.

I hope you got it.


Also, Go Through this[^]


Cheers
 
Share this answer
 
v2
Comments
Member 4228925 21-Mar-15 11:47am    
Got it..thank you
Magic Wonder 23-Mar-15 1:42am    
Welcome...!!!
There are - at least - few reasons of slow query execution, such bad data structure, no indexes, too much indexes, etc.

I'd suggest to read this: How To: Optimize SQL Queries[^] and this: Query Performance[^]
 
Share this answer
 
Maybe you could try to put an index on your ndate column? Does it have one already?
 
Share this answer
 
Comments
Member 4228925 8-Mar-15 16:02pm    
There is no Index right now and cant put that as well for several reason. At this moment i want to know exactly why fetching much less record using where clause is taking much longer time.
phil.o 8-Mar-15 16:09pm    
Obviously because filtering is an expensive process, especially if there is no index on the column used for filtering.
Selecting without filtering is just reading data, and it's very fast.
phil.o 8-Mar-15 16:33pm    
Oh, and thanks for the 1-vote by the way. I'll stop answering you from now.
Santosh K. Tripathi 8-Mar-15 23:57pm    
5+ from me :)
phil.o 9-Mar-15 7:07am    
Thanks for your support :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900