Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

SQL Query Parsing

How does SQL Server 2008 parse the following example? Does it first build all the relationships in the JOIN statement, and then record by record starts looking at the conditions in the WHERE clause?

Theory 1:
For example, it starts with record 1, and first checks if T1.Age > 25. If true, it checks the next condition in the where clause. If its false, it skips the rest of the conditions and then moves on to the next record?

Theory 2:
Does it first retrieve all records which have T1.Age > 25? Next it checks all records Which have T2.Salary > 50000. So on and so on?

FROM Table1 T1 JOIN Table2 T2 ON T1.Id = T2.Id
WHERE T1. Age > 25
AND T2.Salary > 50000
AND T1.Disable = 0
And T1.Id NOT IN (SELECT Id From T3)
  • 4
  • 4
  • 2
4 Solutions
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
SQL Server processes queries as a SET, meaning the entire output rowset, and not record-by-record.

From Microsoft's article on Order of Execution (requires Microsoft account login) (likely found in a hundred of other places too)..:

2. ON
11. TOP

So (doing some big summarizing here) .. it processes the FROM and JOIN tables first, then the WHERE, then does the GROUPing, then everything in the SELECT is second to dead last, followed by ORDER BY.
pzozulkaAuthor Commented:
I'm only focused on #4, WHERE clause. Is there a specific order, conditions are checked in the where clause -- top to bottom? (Ex. 1st condition in the where clause is checked first)?

I was told that this might be the case.
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
No.  The WHERE clause will process as a whole, from left to right, unless you include parentheses ( ) to force an explicit order of execution.

Having said that though, the answer could change based on the indexes you have on that table.  If one column in the WHERE is indexed and the others are not, it will probably start with that column first.

If you have the ability to create indexes on this table, to insure the fastest execution consider creating a covering index that contains all columns in your SELECT and WHERE clause
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Scott PletcherSenior DBACommented:
>>  The WHERE clause will process as a whole, from left to right, unless you include parentheses ( ) to force an explicit order of execution. <<

I don't believe that's true, and you certainly can't rely on it.

SQL may decide to "short-circuit" and do certain comparisons first, or it may "pre-analyze" some.  You have no control over that, really; even parentheses only force SQL to use the order in determining whether a given expression is true or not, not necessarily the order in which to process the individual conditions.
pzozulkaAuthor Commented:
But generally speaking, query parsing is done one row at a time, right?
Scott PletcherSenior DBACommented:
No.  It's all parsed ("interpreted") as a unit.

SQL itself will ultimately have to test each row for the specified conditions, but it could do it in a lot of different orders and different ways.

UDB (IBM's DBMS) will completely rewrite parts of the logic for efficiency, changing conditions to functionally equivalent, but more efficient, conditions.

SQL Server doesn't do nearly as much of that -- at least not yet -- but you can't count on SQL doing anything in a specific order or in a particular way, except as already noted for CASE WHEN conditions.
pzozulkaAuthor Commented:
Sorry that's what I meant.

SQL itself will ultimately have to test each row for the specified conditions

I understand the SQL query (actual code) will be interpreted as a unit. What I meant was, once the query (code) is analyzed, it will be optimized by the query optimizer (ex. WHERE conditions will be reordered for improved efficiency), but at the end of the day, once that's done, the records (rows) themselves are being checked one-by-one, right?
Scott PletcherSenior DBACommented:
Ultimately, presumably so.  But you'll need such a thing in a query plan, for example.

With RDBMSs, you need to think in terms of actions on sets of rows, not on an individual row by row basis.
pzozulkaAuthor Commented:
Can you please elaborate further on what you mean by:
think in terms of actions on sets of rows
I've been writing SQL queries for the past 5 years, but this is the first time where I have to write them with performance in mind. This means, instead of simply thinking about getting results, I now have to think in terms of how the results are retrieved.
Scott PletcherSenior DBACommented:
>> But you'll need such a thing in a query plan, for example. <<

But you'll never see such a thing [row by row comparisons of table columns] in a query plan, for example.

Yes, you have to make sure you don't write the SQL code such that it forces poor performance.  For that you must look at the query plan.  If it contains too many scans, esp. full table scans and/or too many bad row count estimates on large tables and/or too many other problems, such as implicit conversions or key columns not being used when they should be, you have to adjust the SQL to correct those issues.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now