"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

November 11, 2009

SQL Query Execution

SQL Query Execution Phase Involves below Stages

1. Parsing
2. Normalization
3. Optimization
4. Caching
5. Wait for Memory to Execute the Plan
6. Execute
7. Return Results


  • Parsing - Validate Syntax of Query, Split Query into Operators, Expressions, Keywords. Output is Parse Tree.
  • Normalization - Validate Objects, Replaces views by definitions, Algebrized tree is output. This is input to Optimizer to generate execution Plan.
  • Optimizer - Responsible for generating Query Plan. Cost Based Optimizer, based on Table Statistics, Indexes, JOIN selection. Trivial or Straight Forward Optimization and Full Optimization are two phases involved in identifying Optimal Plan.
  • Compiled Plan is Output from Optimizer. The compiled plan for this query, though, would tell SQL Server exactly which physical query operators to use. Compiled plans are reentrant, which is to say that if multiple users are simultaneously executing the same stored procedure, they can all share a single compiled plan.
  • Execution contexts - Information Specifix to Particular user, Execution. Cannot be Shared simultaneously. Every Execution Context is linked to a compiled Plan.
  • SQL Server breaks queries down into a set of fundamental building blocks that we call operators.
  • Operators can be either Physical or Logical. JOIN is a logical operation wheras nested loop join is a Physical Operator.
  • Physical Operators implement operation defined by logical operators. Physical Operators answers three method calls INIT() - Initialize itself with required data structure. GETNEXT() - Call first, next subsequent rows, CLOSE() - Clear up operations.
  • Few Tricky Physical Operators
    • Lazy spool - . The Lazy Spool logical operator stores each row from its input in a hidden temporary object stored in the tempdb database.
    • Spool - The Spool operator saves an intermediate query result to the tempdb database.
  • Caching - Plans are Cached in Cached stores for SPs, Functions, Adhoc Query Plans, Auto Parameterized Plans
You can also check recorded session on Execution Plan Anaysis in SQLCommunity site link
Intepreting Execution Plans

Why should I create an index?
I Smell a Parameter!
Microsoft SQL Server Execution Plans: From Compilation, to Caching, to Reuse
SQL Query Execution Notes

Happy Learning!!!!

1 comment:

Hernan Martin said...

Great article. So this is the physical reordering of the logical query processing?

Hernan Martin