Solved

SQL Query Parsing

Posted on 2014-03-05
10
309 Views
Last Modified: 2014-03-19
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?

SELECT *
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)
0
Comment
Question by:pzozulka
  • 4
  • 4
  • 2
10 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 125 total points
ID: 39908379
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)..:

1. FROM
2. ON
3. OUTER
4. WHERE
5. GROUP BY
6. CUBE | ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
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.
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39908398
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.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39908409
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
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 375 total points
ID: 39910268
>>  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.
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39911026
But generally speaking, query parsing is done one row at a time, right?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 375 total points
ID: 39911138
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.
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39911225
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?
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 375 total points
ID: 39911312
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.
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39912849
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.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39941090
CORRECTION:
>> 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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now