Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»»

Statement evaluation precedence Expand / Collapse
Author
Message
Posted Friday, May 7, 2010 5:41 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, August 18, 2014 9:20 AM
Points: 1,264, Visits: 3,567
So, now knowing how a query is processed internally, how can we use this knowledge to improve query performance or perhaps troubleshooting unexpected results?

______________________________________________________________________________________________
Forum posting etiquette. Get your answers faster.
Post #917820
Posted Friday, May 7, 2010 7:21 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:05 PM
Points: 2,553, Visits: 3,800
Here's a link to similar info:

http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/

The difference between a logical vs internal implementation of code is moot because we have little control over how the engine determines to run it. It's an interesting academic discussion but does it have any practical use when writing code?

I like the question, though.
Post #917937
Posted Friday, May 7, 2010 7:51 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, July 22, 2011 7:33 AM
Points: 289, Visits: 433
This was a great question. I just added "The Logical Query Processing Diagram" to my toolkit. Thanks for the link.
Post #917969
Posted Friday, May 7, 2010 8:02 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, September 27, 2011 12:26 PM
Points: 383, Visits: 14
Even when I miss the answer I still love learning what the answer is.
Thanks for this gem!

I'll see if I can parley this into better queries in future.
Post #917984
Posted Friday, May 7, 2010 8:33 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, April 25, 2014 8:31 AM
Points: 513, Visits: 430
skjoldtc (5/7/2010)
Here's a link to similar info:

http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/

The difference between a logical vs internal implementation of code is moot because we have little control over how the engine determines to run it. It's an interesting academic discussion but does it have any practical use when writing code?
I agree with the Bob's comment on the link above and Hugo's explanation here. I would guess the practical thing would be to know these Phases and their order but not overly rely on it for query execution. It would be good for a beginner to know how late in the query execution is DISTINCT, for example, is taken into account and how many rows have already been parsed/processed. But the beginner should also learn to look at execution plans and read the predicates to see the efficiency of the query.

All questions are good questions for me (b'cos I've never submitted one myself)
Post #918016
Posted Friday, May 7, 2010 9:50 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 1:37 PM
Points: 21,639, Visits: 15,309
Paul White NZ (5/7/2010)
CirquedeSQLeil (5/6/2010)
Though I got this correct, it appears there is a typo in the correct answer. My version had the last two flip-flopped based on the source document provided in the answer.

What are you referring to here Jason?
It must be hard to spot - I can't see anything wrong...


Your list has it correct, Itzik's list has it correct. The correct Answer (and explanation) has inverted the order by and top (order by then top as opposed to the document which says top then order by).




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #918096
Posted Friday, May 7, 2010 9:51 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 1:37 PM
Points: 21,639, Visits: 15,309
Tom Brown (5/7/2010)
Thank-you all.

I've had this data for 10 years now, maybe I copied it from someone's SQL 2000 book - or possibly it came from an SQL 7 course I did (in 1999!), and I found it really useful, But I was slightly unsure that it was still valid, particularly as I had some difficulty in finding a concrete reference.


Thanks Paul for the tip on Itzik's book - I'll be visiting Amazon soon I think (or should I wait for the R2 edition?)




Your link to Pinal Daves website is an excerpt taken from Itziks book for 2008.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #918098
Posted Friday, May 7, 2010 9:58 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 1:37 PM
Points: 21,639, Visits: 15,309
skjoldtc (5/7/2010)
Here's a link to similar info:

http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/


This is the alternate blog site for Pinal Dave. Both link to the same poster for download.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #918104
Posted Friday, May 7, 2010 10:06 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 10:36 AM
Points: 2,140, Visits: 3,421
I actually thought this was a great question, because it made me think. I looked at my choices and said to myself, "okay, let's say I'm processing this. What order would make the most sense?"

I looked at the choices, thought about what seemed most logically correct, and made a somewhat educated guess. Lo and behold, I got it right!
Post #918113
Posted Friday, May 7, 2010 11:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:22 PM
Points: 5,975, Visits: 8,233
CirquedeSQLeil (5/7/2010)
Paul White NZ (5/7/2010)
CirquedeSQLeil (5/6/2010)
Though I got this correct, it appears there is a typo in the correct answer. My version had the last two flip-flopped based on the source document provided in the answer.

What are you referring to here Jason?
It must be hard to spot - I can't see anything wrong...


Your list has it correct, Itzik's list has it correct. The correct Answer (and explanation) has inverted the order by and top (order by then top as opposed to the document which says top then order by).


Hi Jason,

It's not my habit to disagree with Itzik, but in this case I have to make an exception.

Lemme ask you a simple question - in a query that has a TOP and an ORDER BY clause, how exactly should SQL Server process the TOP without sorting the rows first? *IF* Microsoft had decided to use a seperate clause to define the order for the TOP instead of overloading the ORDER BY clause (for instance TOP ... OVER (ORDER BY column)), then I would agree with TOP first (which includes ordering by the OVER (ORDER BY) specificion of the TOP), ORDER BY (for presentation) last. But as it stands, it simply does not make sense to do an ORDER BY after a TOP that has already included the same sort operation.

I disagree with the list and Itzik, and I agree with the order as presented in the question.

Note that, for most clauses, the logical processing order is defined by the ANSI standards. However, the TOP clause is proprietary; ANSI does not support it. And Microsoft never officially announced the logical order, so this is all speculation. Both on Itzik's and on my part.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #918238
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse