Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

order of SQL execution Expand / Collapse
Author
Message
Posted Friday, February 8, 2008 8:09 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 3, 2011 9:11 AM
Points: 64, Visits: 104

I have a query

Select * from Table1 where
Filter1>100 and Filter2= "test" and filter3< 100

Can anyone tell me how would this query executed?

Will Filter1 will be filtered first or filter 3? Reason i want to know is because if filter1 is getting executed first then
i would put that filter which would reduce more number of records.

Same question i have for "OR" condition

Select * from Table1 where
Filter1>100 or Filter2= "test" or filter3< 100

Thanks,
Ramesh.
Post #453261
Posted Friday, February 8, 2008 8:16 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 11:23 PM
Points: 33,278, Visits: 15,449
You can view the execution plan in SSMS for the query and see the order in which things are applied. The short answer is "it depends". The optimizer will consider # of records, based on statistics, and indexes and it could do either one first.

The way you write it is irrelevant.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #453268
Posted Friday, February 8, 2008 12:00 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, Visits: 409
Hi,
the order of the where condtions doesn't play interms which one to execute first. It depends on the columns which you are using in where condition has indexes or not.
Thanks -- Vj
http://dotnetvj.blogspot.com


Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



Post #453403
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse