Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL 202-Theoretical Exection Orders

Posted on January 10, 2011February 9, 2011 by slowder
Looks like we chose the blade.
Looks like we chose the blade.

When you send a query to the server, there’s an order to the steps the server will take to retrieve the data you’re looking for.  It’s important to have an understanding on what order those steps are, so you know where to start making changes in order to minimize the time it takes to optimize the query.

If you start narrowing your results sooner in the execution plan, your overall query will run faster.  Let’s look at the theoretical execution plan orders. There are two for SQL server.  One is the “standard” the other is used when you Use UNION in your query.

The Standard Execution Order

1. FROM, JOIN, ON — Joins that limit your results first.  Try to find ways to limit the results by your JOIN statements or your ON clauses.

2.  WHERE — The next best place to limit the number of results will be your WHERE clause.  I’ve seen many queries try to limit the results with the TOP clause, rather than choosing a smaller result set in the WHERE clause.  That’s never faster. (and yes that is an absolute to me)

3. GROUP BY and aggregate functions — Grouping can be used to get a DISTINCT list, and sometimes that’s useful, but if you can eliminate rows through the previous three steps, your query will be faster overall.

4. HAVING — Filtering based on aggregate values happens after the GROUP BY has taken effect ( you can’t very well have aggregates before the grouping has happened, right?

5.  SELECT — This is where we limit the results to just the columns we want to see.  You’re limiting the “width” of the results here, not the number of rows.

6.  ORDER BY — your results are sorted, you’re not going to use this to limit your results, so knowing that this comes after the other steps doesn’t help you optimized the query… but you do need to know this step comes before the TOP statement.

7.  TOP — This is the last filter that’s applied.  As a result it’s the least useful to use to speed up a query.  I’m not going to say affecting the results by limiting it to the TOP x results won’t help…but there are better ways to limit your results (FROM/JOINs, WHERE, and HAVING).

8. FOR XML — the results are converted to XML.  This has to come after everything else.  You have to have the results before you can transform them to XML.  There’s no speed to be gained in this step.  Just know it’s last.

The Execution Order when UNIONs are Applied

The order changes when you use a UNION.  FROM, JOIN, and ON are still first, WHERE is next, Then GROUP BY, and HAVING still comes in fourth.  But that’s where it changes when you use UNION.

5. TOP — all of your query filters have to fire before the server computes the UNION.

6. UNION and SELECT — all the results of all the SELECT statements are put together, then the SELECTed columns are returned.

7.  ORDER BY comes next.  Since you have to have all the results before you can ORDER the results, it has to come after the SELECT step, just like the Standard Execution plan.

8.  FOR XML — it’s still last.

Basically TOP moves ahead of SELECT when UNIONs are in play.  It helps narrow the number of rows more when you’re dealing with what would be multiple row sets.

What’s it all About?

First, thanks for not calling me Alfie.

Basically, it should help you know where to start tuning your queries.  Look at your FROM and JOIN statements first.  Are you choosing more tables than you need.  You’re looking up names and numbers, why do you have a join to a table with cars in it?  Drop it, and watch your query speed up!

Next, look at your WHERE clause, can you be more specific about what you’re looking for?  Could you narrow it down by using an integer column instead of a VARCHAR(MAX) field?  Either way, you can speed up your query with one of these.

HAVING, then TOP… These are my last two resorts for tuning a query by rewriting it.  Usually if I can’t solve the issue through tuning the FROM, JOIN, or WHERE, I turn to indexes.  And that’s what I’m going to go into next time.  If you just keep this summary section in your mind, you’ll get by just fine.

If you have any questions send them in!  I’m here to help!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme