Skip to main content

SQL Server Query Processing & Execution

How SQL Server takes your T-SQL query and turns it into a result set - parsing, optimization, and execution.

SQL Server Query Processing Pipeline

Query Processing Pipeline

From T-SQL to Result Set
*
1. Parse
Syntax check
Tokenize T-SQL
Build parse tree
No table validation yet
*
2. Bind (Algebrize)
Resolve object names
Check permissions
Type checking
Produce algebrizer tree
*
3. Optimize
Cost-based optimizer
Considers join order, indexes
Generates candidate plans
Picks lowest-cost plan
*
4. Execute
Follow execution plan
Iterator model (pull-based)
Each operator requests rows
Result streamed to client

The Query Optimizer

SQL Server uses a cost-based optimizer - it estimates the cost of different execution strategies and picks the cheapest one.

Optimizer Key Concepts
*
Statistics
- Histogram on column values
- Used to estimate row counts (cardinality)
- Auto-created on indexed columns
- Auto-updated after ~20% change (pre-2016)
- Lower dynamic threshold in 2016+
- Stale stats = bad plans
*
Cost Estimation
- Units are relative (not seconds)
- Based on CPU + I/O costs
- Depends on estimated cardinality
- Wrong estimates -> wrong plan
- Check: SET STATISTICS IO ON

Execution Plan Types

Plan TypeDescriptionWhen Used
Trivial planNo optimization needed - obvious best planSimple queries (single table, no joins)
Full optimizationCost-based search across plan spaceComplex queries with joins, subqueries
Parallel planMultiple threads execute operatorsWhen cost > cost threshold for parallelism (default: 5)
Best Practice

Read execution plans right-to-left, top-to-bottom. Data flows from right to left. Look for: thick arrows (many rows), Key Lookups (missing index), Sort operators (memory grant), and warnings (yellow triangles). Reference: Microsoft Learn: Execution Plan Guide

Parameter Sniffing

One of the most common performance problems in SQL Server.

Parameter Sniffing Explained
OK
Good Sniffing (Normal)
- First execution compiles plan
- Plan optimized for first parameter value
- Cached and reused for all values
- Works well when data is uniformly distributed
*
Bad Sniffing (Problem)
- First parameter has skewed data
- Plan optimal for 10 rows, bad for 1M rows
- All subsequent calls use the bad plan
- Symptoms: intermittent slow queries

Solutions for bad parameter sniffing:

ApproachHowTrade-off
OPTION (RECOMPILE)Force new plan each executionCPU cost of recompilation
OPTION (OPTIMIZE FOR UNKNOWN)Use average stats instead of sniffed valueMay not be optimal for any value
Plan guidesForce a specific planMaintenance overhead
Query Store plan forcingPin a known-good planRequires Query Store enabled
🔀 Oracle → Azure

Oracle parallel: Parameter sniffing is equivalent to Oracle's adaptive cursor sharing problem. Oracle 11g+ has Adaptive Cursor Sharing to handle this automatically. SQL Server has no automatic equivalent - you must diagnose and fix it manually or use Query Store.

Query Store

Query Store is SQL Server's built-in flight recorder for query performance. Available since SQL Server 2016.

Query Store Capabilities
*
Performance Tracking
- Captures query text, plans, runtime stats
- Historical comparison
- Regressed queries report
- Top resource consumers
*
Plan Forcing
- Pin a known-good plan
- Survives restarts
- No plan guides needed
- Per-query granularity
*
Automatic Tuning
- Auto plan correction (2017+)
- Detects plan regression
- Forces last known good plan
- sys.dm_db_tuning_recommendations
🎯 Exam Focus

Query Store is the recommended replacement for plan guides, trace flags, and manual plan cache analysis. It persists through restarts (stored in the database), provides historical data, and enables automatic plan regression correction (SQL Server 2017+). Reference: Microsoft Learn: Query Store

Intelligent Query Processing (IQP)

Starting with SQL Server 2019, the engine includes automatic performance improvements:

FeatureWhat It DoesVersion
Adaptive JoinsSwitch between nested loop and hash at runtime2017+
Memory Grant FeedbackAdjust memory grants based on actual usage2017+ (batch), 2019+ (row)
Table Variable Deferred CompilationBetter cardinality for table variables2019+
Batch Mode on RowstoreUse batch processing without columnstore2019+
Approximate Count DistinctAPPROX_COUNT_DISTINCT - fast, ~2% error2019+
Parameter Sensitive Plan Optimization (PSPO)Multiple plans for different parameter ranges2022+ (compat level 160)
Best Practice

To get IQP features, set database compatibility level to 150 (SQL 2019) or 160 (SQL 2022). You don't need to upgrade the engine - just the compat level. But test thoroughly - some query behaviors change with newer compat levels. Reference: Microsoft Learn: IQP


Flashcards

What are the 4 phases of query processing?
Click to reveal answer
1. Parse (syntax check) -> 2. Bind/Algebrize (resolve names, check permissions) -> 3. Optimize (cost-based plan selection) -> 4. Execute (iterator model, pull-based).
1 / 6