views:

49

answers:

1

Hi friends,

In the book Inside Microsoft SQL Server™ 2005 T-SQL Querying, the author Itzik Ben-Gan tell us the order or SQL Server 2005's logical query processing is:

(8)  SELECT (9) DISTINCT (11) <TOP_specification> <select_list>
(1)  FROM <left_table>
(3)       <join_type> JOIN <right_table>
(2)       ON <join_condition>
(4)  WHERE <where_condition>
(5)  GROUP BY <group_by_list>
(6)  WITH {CUBE | ROLLUP}
(7)  HAVING <having_condition>
(10) ORDER BY <order_by_list>

1.  FROM
2.  ON
3.  OUTER (join)
4.  WHERE
5.  GROUP BY
6.  CUBE | ROLLUP
7.  HAVING
8.  SELECT
9.  DISTINCT
10. ORDER BY  <---------------------- NOTE
11. TOP       <---------------------- NOTE

In his book Inside Microsoft SQL Server 2008 : T-SQL Querying, he tell us the following logical query processing order:

(1) FROM
(1-J1) Cartesian Product
(1-J2) ON Filter
(1-J3) Add Outer Rows
(2) WHERE
(3) GROUP BY
(4) HAVING
(5) SELECT
(5-1) Evaluate Expressions
(5-2) DISTINCT
(5-3) TOP       <---------------------- NOTE
(6) ORDER BY    <---------------------- NOTE

Note the order of TOP and ORDER BY in the upper excerpts from these books. They are just opposite. I think these two step is very important, and will give a totally different result with different order. I want to know whether SQL Server 2008 changed something in it's storage engine from SQL Server 2005 or something else reason cause this?

Thanks.

+3  A: 

Check this out - it's an expose on this issue - and Itzik's book is mentioned. The second order above is correct.

Will A
Note that the TOP uses the ORDER BY in determining the rows to keep. This is the logical processing model - so getting the TOP n records based on the ORDER BY _logically_ doesn't result in a rowset with a particular ordering - the ORDER BY logical step orders the results. In terms of physical processing, the ORDER BY would be performed prior to the TOP n.
Will A