Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL Query Parsing

Posted on 2014-03-05
10
Medium Priority
?
364 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 66

Accepted Solution

by:
Jim Horn earned 500 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 66

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1500 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
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1500 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1500 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 70

Expert Comment

by:Scott Pletcher
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

572 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