Where clause execution sequence

Source: Internet
Author: User

Check the Code:
Copy codeThe Code is as follows:
Set statistics io on
Set statistics time on
Go
Set statistics profile on
Go

Use pubs
Select * from authors
Where (au_fname like's % 'or au_fname like 'a % ')
And (state like 'CA' or state like 'ut ')
And (cast (zip as int) & gt; 90000)

Select * from authors
Where (au_fname like's % 'or au_fname like 'a % ')
And (cast (zip as int) & gt; 90000)
And (state like 'CA' or state like 'ut ')

Select * from authors
Where (Stream (zip as int)> 90000)
And (au_fname like's % 'or au_fname like 'a % ')
And (state like 'CA' or state like 'ut ')

Objective: To verify the query sequence of the where statement's conditions
Environment: SQLServer 2005Express
Step: display the query plan
Result: The code is unified as follows:
Copy codeThe Code is as follows:
| -- Clustered Index Scan (OBJECT :( [pubs]. [dbo]. [authors]. [UPKCL_auidind]), WHERE :( CONVERT (int, [pubs]. [dbo]. [authors]. [zip], 0)> (90000) AND ([pubs]. [dbo]. [authors]. [au_fname] like's % 'OR [pubs]. [dbo]. [authors]. [au_fname] like 'a % ') AND ([pubs]. [dbo]. [authors]. [state] like 'CA' OR [pubs]. [dbo]. [authors]. [state] like 'ut ')))

Conclusion: In versions earlier than 2005, the order of the where clause is as follows. However, it seems that the version 2005 has been optimized, and all the order is unified into statements sorted by filtering capability.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.