SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Statement evaluation precedence


Statement evaluation precedence

Author
Message
calvo
calvo
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3274 Visits: 4018
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.
OCTom
OCTom
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4127 Visits: 4152
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.
sdorris-1150234
sdorris-1150234
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 433
This was a great question. I just added "The Logical Query Processing Diagram" to my toolkit. Thanks for the link.
david.j.campbell
david.j.campbell
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
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.
rjv_rnjn
rjv_rnjn
SSC Eights!
SSC Eights! (957 reputation)SSC Eights! (957 reputation)SSC Eights! (957 reputation)SSC Eights! (957 reputation)SSC Eights! (957 reputation)SSC Eights! (957 reputation)SSC Eights! (957 reputation)SSC Eights! (957 reputation)

Group: General Forum Members
Points: 957 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) :-)
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66261 Visits: 18570
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...Unsure


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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66261 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66261 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Ray K
Ray K
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6710 Visits: 4676
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!

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18745 Visits: 12426
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...Unsure


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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search