top of page

Query Execution Plans for SQL Server Optimization


This guide is designed for those who seek not just to follow the map, but to truly comprehend and construct it. We will lead you through the intricate path of query execution plans, demystifying each turn for effective performance tuning. We start with the fundamentals, progressively climbing to the advanced strategies that allow for fine-grained control over SQL Server performance.

The Foundation: Basics of Query Execution Plans

At the heart of SQL Server's capability to execute your queries with efficiency lies the query execution plan. It's a visual roadmap that SQL Server's query optimizer creates to represent how a SQL statement is processed.

Each plan is a combination of elements known as operators, properties, and estimates, which together lay out the operations the server will perform—like table scans, index seeks, or merges - to return your requested data.

Generative Processes

SQL Server can generate two different types of execution plans:

  • Estimated Plans: These are plans generated without executing the actual query. They're based on current database statistics and provide a forecast of how the query will perform.

  • Actual Plans: These plans are the real deal, generated after the query is run. They detail the actual path SQL Server took and what resources it actually used.

Anatomy of an Execution Plan


The anatomy of an execution plan in SQL Server provides a comprehensive view of how the database engine processes a query. An execution plan typically includes the following components:

Query Tree: The execution plan is visually represented as a tree structure, where each node denotes a specific operation performed by SQL Server in the query execution process.

Operators: Nodes within the execution plan represent individual operations executed by SQL Server. These operators can include tasks like scanning tables or indexes, performing joins, filtering rows, sorting data, and aggregating results.

Access Methods: These methods outline how SQL Server accesses data from tables or indexes. Common access methods include index seeks, index scans, table scans, clustered index seeks, and clustered index scans.

Join Types: Join operators indicate how SQL Server combines rows from different tables. Examples of join types include nested loops joins, merge joins, and hash joins, each with distinct characteristics and performance implications.

Predicates: Predicates represent conditions used to filter rows during query execution. These conditions typically originate from WHERE clause conditions, JOIN conditions, and additional filters applied during query processing.

Estimated and Actual Execution Costs: Each operator in the execution plan includes estimated and actual execution costs, indicating the relative resource consumption of the operation in terms of CPU, memory, and I/O. These costs help assess the efficiency of the chosen execution plan.

Data Flow: Execution plans illustrate the flow of data between operators, depicting how data is passed from one operation to another within the query plan.

Parallelism: Some execution plans may include parallelism operators, indicating instances where SQL Server employs multiple threads to execute portions of the query concurrently. Parallelism can enhance query performance by utilizing multiple CPU cores.

Warnings and Messages: Execution plans may contain warnings or messages generated during query optimization or execution. These messages provide insights into potential issues encountered by SQL Server during query processing.

Memory Grants: Certain execution plans provide information about memory grants allocated to the query for memory-intensive operations like sorting or hashing. Understanding memory grants helps optimize memory usage and query performance.

These components collectively offer a detailed understanding of how SQL Server processes queries and help database administrators and developers identify performance bottlenecks and optimize query performance.

Building Blocks: Generating and Viewing Execution Plans

To harness the power of execution plans, it’s essential to know how to summon and inspect them. We'll cover various methods to generate and decipher these plans using SQL Server Management Studio (SSMS) and other robust tools at your disposal.

Summoning a Plan with SSMS

Step-by-Step Guide

Using SSMS, you can view the execution plan of any query with these simple steps:

  • Open a query window.

  • Write or paste your query into the window.

  • Click 'Query' in the top menu.

  • Select 'Include Actual Execution Plan'.

  • Execute your query.

  • View the 'Execution Plan' tab beside the 'Results' tab in the bottom window pane to see the query plan graph.

Advanced Techniques

With SQL Server Profiler and Extended Events, you can capture and analyze the execution plans on a more profound level. This comes in handy when you’re dealing with production systems or need to observe many query plans at once.

Additionally, the use of Trace Flags can be instrumental in particular scenarios. For example, enabling Trace Flag 2861 provides extensive information about statistics and helps troubleshoot poorly performing queries.

To set Trace Flag 2861 in SQL Server, you can use the DBCC TRACEON command. Trace flags are used to enable specific behaviors or debugging features in SQL Server. Trace Flag 2861, in particular, disables the generation of automatic statistics for temporary tables and table variables.

Here's how you can set Trace Flag 2861:

DBCC TRACEON (2861, -1);

In this command:

  • DBCC TRACEON is the command used to enable a trace flag.

  • 2861 is the specific trace flag number you want to enable.

  • -1 parameter indicates that the trace flag should be enabled globally for all connections.

Alternatively, you can also enable the trace flag at the startup of SQL Server by adding it to the startup parameters. Here's how you can do it:

  • Open SQL Server Configuration Manager.

  • Right-click on the SQL Server instance you want to modify, and select Properties.

  • Go to the Advanced tab.

  • In the Startup Parameters field, add -T2861.

  • Restart the SQL Server service for the changes to take effect.

Decoding the Plan: Interpreting Execution Plans

Merely viewing an execution plan is not enough. You must interpret each component to gain insights into how SQL Server processes the query. This section dives deep into the visual cues and data points that can unlock optimization opportunities.

Meaning Behind Operators

Here's an expanded list of query plan operators commonly found in SQL Server execution plans:

  • Nested Loops Join: Performs a nested loop join between two input sets, iterating through each row of the outer input and finding matching rows from the inner input.


  • Merge Join: Sorts the input sets by the join keys and merges them together, efficient when joining two sorted sets.


  • Clustered Index Seek/Scan: Scans or seeks the clustered index of a table to retrieve rows based on the search criteria.

  • Non-clustered Index Seek/Scan: Retrieves rows using a non-clustered index, typically to avoid scanning the entire table.

  • Table Scan: Reads all rows from a table or index without using any indexes, typically less efficient than an index seek or scan.

  • Filter: Applies a filter condition to the input rows, retaining only those that meet the specified criteria.

  • Sort: Sorts the input rows based on one or more columns, commonly used for ORDER BY clauses or to support other operations like merge join.

  • Aggregate: Performs aggregate functions like SUM, AVG, COUNT, etc., on the input rows, producing summarized results.

  • Stream Aggregate: Computes aggregate functions over a stream of sorted input rows, typically used when the input is sorted by the grouping columns.

  • Parallelism (Gather Streams, Repartition Streams, Distribute Streams): Indicates parallel execution of a portion of the query plan, where multiple threads process data concurrently.

  • Index Spool (Eager Spool, Lazy Spool): Stores intermediate results in tempdb to avoid recalculating them during subsequent query plan operations.

  • Compute Scalar: Computes new values based on expressions or functions applied to input rows.

  • Constant Scan: Generates a single-row resultset containing constants specified in the query.

  • Window Aggregate: Performs aggregate functions over a window of rows defined by the OVER clause in analytical queries.

  • Segment: Identifies segments in the input rows for partitioned window functions.

  • Top: Retrieves the top N rows from the input rows, commonly used with the ORDER BY clause.

  • Table-valued Function: Applies a table-valued function to the input rows, producing a new set of rows.

These operators represent the fundamental building blocks of SQL Server execution plans, each designed to efficiently perform specific tasks during query execution. Understanding these operators helps in interpreting and optimizing query execution plans effectively.

Diving Deeper: Identifying Performance Bottlenecks

Execution plans are effective tools for identifying bottlenecks. This section highlights the common issues revealed through query plans and the steps you can take to resolve them.

Identifying performance bottlenecks in query plans involves recognizing areas where SQL Server may be performing suboptimally, leading to slow query execution. Here are some key aspects to consider:

Missing Indexes:

  • Identification: Look for operators like "Table Scan" or "Index Scan" in the execution plan, indicating full scans of tables or indexes.

Resolution: Use the Missing Indexes feature in SQL Server Management Studio (SSMS) or dynamic management views (DMVs) to identify missing indexes suggested by the query optimizer. Evaluate the impact of creating these indexes on overall workload performance and disk space usage. Consider creating necessary indexes on columns frequently used in WHERE clauses, JOIN conditions, and ORDER BY clauses. Inefficient Joins:

  • Identification: Analyze join operators such as "Nested Loops Join," "Merge Join," or "Hash Match Join" in the execution plan.

  • Resolution: Evaluate query join strategies and consider rewriting queries to use more efficient join types. Ensure that necessary indexes are in place to support efficient join operations. Use query hints or optimizer hints (e.g., LOOP, MERGE, HASH) to influence the join strategy if necessary. Experiment with indexing strategies to optimize join performance, such as covering indexes or indexed views.

Expensive Sort and Aggregate Operations:

  • Identification: Look for Sort and Aggregate operators in the execution plan, particularly if they involve large datasets.

  • Resolution: Consider whether sorting or aggregation can be performed more efficiently, possibly by restructuring the query or introducing appropriate indexes. Evaluate the necessity of sorting or aggregating data at the database level versus handling it at the application level. Implementing indexed views, materialized views, or pre-aggregated tables may also help optimize such operations.

Excessive Parallelism:

  • Identification: Monitor parallelism-related operators in the execution plan, such as "Parallelism" or "Parallel Scan."

  • Resolution: Adjust server-level settings (e.g., MAXDOP, Cost Threshold for Parallelism) to control parallelism and prevent excessive parallel plan generation. Use query hints (e.g., MAXDOP) to influence parallelism at the query level. Consider breaking down complex queries into smaller units or optimizing resource-intensive queries to minimize the need for parallel execution.

Data Skewness and Data Distribution:

  • Identification: Analyze distribution statistics and histograms to identify uneven data distribution or data skewness.

  • Resolution: Evaluate the distribution of data across partitions and consider data partitioning strategies to distribute data more evenly. Update statistics regularly to ensure accurate cardinality estimates and improve query plan quality. Use query hints or plan guides to force specific join or query execution strategies based on data distribution characteristics.

Suboptimal Cardinality Estimates:

  • Identification: Look for discrepancies between estimated and actual row counts in the execution plan.

  • Resolution: Update statistics on relevant tables and indexes to provide the query optimizer with accurate information for cardinality estimation. Use query hints (e.g., OPTION (RECOMPILE)) to force recompilation of query plans with updated statistics. Evaluate the use of query hints or plan guides to influence cardinality estimates for specific queries.

Resource Contention and Blocking:

  • Identification: Identify operators related to resource contention, such as "Parallelism," "Index Spool," or "Sort Spill," in the execution plan.

  • Resolution: Analyze server configurations and resource usage patterns to identify and address potential resource contention issues, such as memory pressure or disk I/O bottlenecks. Optimize queries and indexes to minimize blocking and contention by reducing transaction isolation levels, optimizing locking hints, or redesigning indexes to minimize contention.

8. Redundant or Unnecessary Operations:

  • Identification: Review the execution plan for redundant or unnecessary operations, such as redundant sorts or unnecessary columns in SELECT statements.

  • Resolution: Refactor queries to eliminate redundant operations and streamline data retrieval and processing. Consider optimizing queries to retrieve only the necessary columns and rows, avoiding unnecessary data transfer and processing. Use tools like SQL Server Profiler or Extended Events to identify and eliminate redundant or inefficient query patterns.

Navigating Complex Terrains: Advanced Topics in Query Execution Plans

Optimizer Internals

To truly master query execution plans, you need to understand how the optimizer thinks. We take you through the black box that is the SQL Server query optimizer's decision-making process.

The Cost-Based Optimizer

The Cost-Based Optimizer (CBO) is a component of SQL Server's query processing engine responsible for generating and selecting the most efficient query execution plan based on estimated costs. The CBO evaluates various potential execution plans for a given query and selects the one with the lowest estimated cost. The cost estimation considers factors such as the number of rows processed, I/O operations, memory usage, and CPU utilization.

By analyzing the execution plan, developers and database administrators can gain insights into how SQL Server processes the query and identify opportunities for performance optimization, such as adding indexes, rewriting queries, or adjusting server configurations.

Statistics and Cardinality Estimation

Statistics and cardinality estimation play a crucial role in SQL Server query optimization and execution plans. Here's an explanation of each:

Statistics: Statistics in SQL Server are metadata objects that contain information about the distribution of data within tables and indexes. These statistics help the query optimizer make informed decisions about the most efficient query execution plan.

  • Column Statistics: SQL Server maintains statistics for individual columns, storing information such as the number of distinct values, the minimum and maximum values, and the distribution of values within the column.

  • Index Statistics: For indexed columns, SQL Server also maintains statistics on the distribution of key values within the index. These statistics help the optimizer evaluate the selectivity of index seeks and scans.

Cardinality Estimation: Cardinality estimation refers to the process of estimating the number of rows that will be returned by a query operation. The query optimizer uses cardinality estimates to evaluate the cost of different query execution plans and select the most efficient one.

  • Row Estimation: Cardinality estimation involves estimating the number of rows produced by each operator in the query plan. This estimation is based on statistics, predicates, join conditions, and other factors.

  • Join Cardinality: In join operations, the optimizer estimates the number of rows produced by each input table and uses this information to determine the join order and join type (e.g., nested loops, merge, hash).

  • Aggregate Cardinality: For aggregate operations (e.g., GROUP BY, DISTINCT), the optimizer estimates the number of distinct groups or values to be produced by the operation.

  • Filter Predicates: Cardinality estimation also considers filter predicates (e.g., WHERE clauses) to estimate the number of rows that will satisfy the predicate conditions.

Impact on Query Plans: Accurate statistics and cardinality estimation are essential for generating optimal query execution plans. Inaccurate statistics or cardinality estimates can lead to suboptimal plans, resulting in inefficient query performance.

  • Underestimation: If the optimizer underestimates the number of rows, it may choose a plan with nested loops joins or index seeks that perform poorly for larger row counts.

  • Overestimation: Conversely, if the optimizer overestimates the number of rows, it may choose a plan with hash joins or parallelism that incurs unnecessary overhead for smaller row counts.

Updating Statistics: To ensure accurate cardinality estimation, it's essential to regularly update statistics on tables and indexes, especially for frequently modified tables or columns with skewed data distributions. SQL Server provides automatic and manual options for updating statistics using commands like UPDATE STATISTICS or by enabling the auto-update statistics feature.

Plan Caching and Reuse


Plan caching and reuse in SQL Server is a critical aspect of query performance optimization, aimed at reducing overhead by storing and reusing execution plans for frequently executed queries. Here's an overview of plan caching and reuse, along with some useful T-SQL commands for managing this aspect of query optimization:

Plan Caching: When SQL Server receives a query, it undergoes a process known as query optimization, where the query optimizer evaluates various potential execution plans and selects the most efficient one based on factors such as cost and resource utilization. Once an optimal plan is generated, SQL Server caches it in memory for reuse, avoiding the need to recompile the query every time it's executed.

Plan Reuse: SQL Server attempts to reuse cached execution plans whenever possible to minimize overhead and improve query performance. Plan reuse can occur in various scenarios:

  • Identical Queries: If a query is exactly the same as one that's already in the plan cache (including whitespace and case sensitivity), SQL Server can reuse the cached plan.

  • Parameterized Queries: For parameterized queries (e.g., stored procedures with parameters), SQL Server can generate a parameterized plan and reuse it for different parameter values, reducing the need for plan recompilation.

  • Statement Level Recompilation: SQL Server may choose to recompile a query statement within a stored procedure while reusing the overall plan for the procedure. This allows for better plan stability while still accommodating changes in specific query conditions.

Useful T-SQL Commands:

Viewing Plan Cache Contents:

SELECT * FROM sys.dm_exec_cached_plans; Clearing Plan Cache:

DBCC FREEPROCCACHE;

This command clears the entire plan cache, forcing SQL Server to recompile all subsequent queries.

Clearing Plan Cache for a Specific Database:

DBCC FREEPROCCACHE (DB_ID);

Replace DB_ID with the ID of the specific database whose plan cache you want to clear.

Clearing Plan Cache for a Specific Query:

DBCC FREEPROCCACHE (plan_handle);

Replace plan_handle with the unique identifier (plan handle) of the specific query execution plan you want to remove from the cache.

Forcing Plan Recompile for a Specific Query:

EXEC sp_recompile 'dbo.TableName';

This command forces SQL Server to recompile the execution plan for all queries referencing the specified table, ensuring fresh optimization.

Best Practices:

  • Avoid excessive plan cache clearing, as it can cause performance degradation due to increased plan recompilations.

  • Monitor plan cache usage and identify queries with high compile times or plan cache pressure for optimization.

  • Use stored procedures and parameterized queries to maximize plan reuse and minimize plan cache bloat.

  • Regularly update statistics to ensure accurate cardinality estimation, leading to optimal plan generation and reuse.

Conclusion: The Journey Continues

Commanding your way through SQL query execution plans is a continuous learning process. The complexities of real-world databases and the ever-evolving landscape of SQL Server ensure that there's always more to explore and understand.

Armed with the knowledge you've gained from this guide, continue to experiment, learn from your experiences, and refine your craft. Your dedication to mastering the intricacies of SQL Server query plans will be rewarded with optimal database performance that paves the way for your applications’ success.

References

Keep on top of the latest in SQL Server optimization. Here are some essential resources to bookmark:

Remember, the more you learn, the sharper your skills will become. Stay focused, experiment, and enjoy the rewards of a finely-tuned SQL Server environment.




Recent Posts

See All

Get in Touch

Thanks for submitting!

Contact Me

1825 Bevery Way Sacramento CA 95818

Tel. 916-303-3627

bottom of page