Execution Plan and its types - SQL Server

In SQL Server, an execution plan is a graphical representation of the query execution process that shows how the SQL Server query optimizer will execute a particular query. The execution plan can help database administrators and developers to understand how a query is being executed and to identify any performance issues.

There are several types of execution plans in SQL Server, including:

Estimated Execution Plan: An estimated execution plan is generated by the query optimizer without actually executing the query. It provides an estimate of the cost of the query execution and can help to identify potential performance issues before running the query.

When generating an estimated execution plan, the query optimizer analyzes the query and creates a plan that is based on statistics about the data in the database. The estimated execution plan shows the operators that will be used to execute the query, the order in which they will be executed, and the estimated number of rows that will be returned at each step.

Actual Execution Plan: An actual execution plan is generated by SQL Server while the query is being executed. It shows the actual resources used during query execution and can help to identify performance bottlenecks.

When generating an actual execution plan, SQL Server executes the query and captures the actual performance metrics for each operator in the plan. The actual execution plan shows the number of rows returned by each operator, the amount of memory and CPU time used, and the duration of the query execution.

Some of the key differences between the estimated and actual execution plans include:

  • The estimated execution plan is generated before the query is executed, while the actual execution plan is generated during query execution.
  • The estimated execution plan provides an estimate of the resources needed to execute the query, while the actual execution plan shows the actual resources used.
  • The estimated execution plan can be generated quickly, while the actual execution plan requires query execution and may take longer to generate.

Live Query Statistics: This is a real-time execution plan that shows the progress of a query as it is being executed. It can help to identify long-running queries or queries that are causing blocking.

Query Store Plan: Query Store is a feature in SQL Server that captures query execution history and provides insights into query performance over time. The Query Store Plan is a type of execution plan that is stored in the Query Store and can be used to compare query performance over time.

Parallel Execution Plan: This is a type of execution plan that is used when a query is executed in parallel on multiple processors. It shows how the query is being divided into smaller tasks and executed in parallel.

Cached Execution Plan: This is a plan that is stored in SQL Server's plan cache after a query has been executed. It can be reused if the same query is executed again, which can improve query performance.


                                                            Display and Save Execution Plans

In SQL Server, you can display and save execution plans using SQL Server Management Studio (SSMS) or by using Transact-SQL. Here are the steps to display and save execution plans using both methods:

Using SSMS:

  • Open SQL Server Management Studio and connect to the SQL Server instance where the query will be executed.
  • Open a new query window and enter the query that you want to generate an execution plan for.
  • Click on the "Include Actual Execution Plan" button in the toolbar or press Ctrl+M to enable the display of the actual execution plan.
  • Execute the query by clicking the "Execute" button or pressing F5.
  • The execution plan will be displayed in a new tab in the query window. You can view the execution plan in graphical or text mode by selecting the appropriate option in the toolbar.
  • To save the execution plan, right-click on the execution plan tab and select "Save Execution Plan As". Choose a location and file name to save the plan.

Using Transact-SQL:

  • Open a new query window and enter the query that you want to generate an execution plan for.
  • Append the "SET STATISTICS XML ON" statement before the query to enable the generation of an XML execution plan.
  • Execute the query by clicking the "Execute" button or pressing F5.
  • The XML execution plan will be displayed in the "Messages" tab of the query window.
  • To save the XML execution plan, copy the XML code to a text editor and save it as an XML file.

Note - The XML execution plan can be opened in SSMS or other tools that support the display of execution plans.

Displaying and saving execution plans can help database administrators and developers to understand query performance, identify performance bottlenecks, and optimize database performance.


                                                            Compare and Analyze Execution Plans

You can compare and analyze execution plans to identify performance issues and optimize query performance. Here are the steps to compare and analyze execution plans:

1. Display the execution plans: Generate the execution plans for the queries that you want to compare, using the steps described in the previous answer.

2. Compare the execution plans: Once you have generated the execution plans, you can compare them side by side in SSMS. To do this, open both execution plans and select the "Compare Showplan" option from the "Query" menu. The two execution plans will be displayed side by side, and you can compare them to identify any differences or similarities.

3. Analyze the execution plans: To analyze the execution plans, you can use the graphical or text mode view in SSMS to identify performance issues. Look for any operators that have high relative cost or that are executed multiple times. These may indicate performance bottlenecks that need to be optimized. You can also use the "Properties" window to view additional information about each operator, such as the estimated and actual number of rows and the resource usage.

4. Optimize the queries: Once you have identified performance issues in the execution plans, you can optimize the queries by modifying the query or the database schema. For example, you might add or remove indexes, adjust the query predicates, or change the join type to optimize query performance.

5. Repeat the process: After optimizing the queries, generate new execution plans and compare them to the original plans to verify that performance has improved. You may need to iterate this process several times to achieve the desired performance improvements.

Comparing and analyzing execution plans is an important step in optimizing query performance and improving database performance. By identifying and addressing performance bottlenecks, you can ensure that your database runs efficiently and provides optimal performance for your users.

Comments

Popular posts from this blog

COPILOT Feature in SQL Server 2025

Prefetching - SQL Server

Split Brain - SQL Server