数据库
首页 > 数据库> > Execution Plans in SQL Server

Execution Plans in SQL Server

作者:互联网

Execution Plans in SQL Server

Introduction

In this article, I’m going to explain what the Execution Plans in SQL Server are and how to understand the details of an execution plan by reading the various metrics available once we hover over the components in the plan.

Being a Data Professional, it is very essential that we must understand how to write efficient queries that return faster results and how to tune the slow performing queries to achieve a boost in performance. In order to understand how the database engine works behind the scenes, we need to know the logical operations performed by the query processor. This can be done by investigating the execution plans generated by the query processor. An execution plan in SQL Server is a simple graphical representation of the operations that the query optimizer generates to calculate the most efficient way to return a set of results.

What is an Execution Plan?

As already mentioned earlier, an execution plan in SQL Server Management Studio is a graphical representation of the various steps that are involved in fetching results from the database tables. Once a query is executed, the query processing engine quickly generates multiple execution plans and selects the one which returns the results with the best performance. There are two types of execution plans to be specific –

  1. Estimated Execution Plan – As the name suggests, this type of execution plan is just a guess by the query processor about how the specific steps that are to be involved while returning the results. It is often generated before the query has been executed
  2. Actual Execution Plan – The Actual Execution Plan is generated after the query has been executed. It shows the actual operations and steps involved while executing the query. This may or may not differ from the Estimated Execution Plan

How to generate Execution Plans?

As we have learned, the Execution Plans in SQL Server can be generated before and after the query has been executed, I’m going to mention the various steps on how you can get the estimated and actual execution plans. 

 

In the execution plan depicted in the above Figure 5, if you hover the cursor over the components, you can view the detailed stats for each of the operations and components being displayed in the execution plan. The plan is interpreted from right-to-left and top-to-bottom. Since our plan consists of only one single row, there is no need for the top-to-bottom approach. 

 

 

     We can distinguish the execution plan into the following five steps:

  1. Clustered Index Scan
  2. Data Flow from Clustered Index Scan (Arrow)
  3. Sort Operator
  4. Data Flow from Sort Operator (Arrow)
  5. Select Operator

For the sake of this tutorial, let’s only understand in detail the various metrics that are being involved for the Clustered Index Scan operator. The details of the other operators can also be viewed similarly; however, the detailed explanation of the metrics of the other operators is beyond the scope of this article. 

 

Clustered Index Scan

The first component when we traverse from right-to-left is the Clustered Index Scan component. This Index Scan is performed on the Primary Key of the table i.e. “PK_Dimension_Customer“. Let’s understand each of the metrics that are being displayed while we hover over the clustered index scan operator. You can refer to the Figure 7, for understanding more. 

 

 

Additionally, if you see below, there are these three properties that tell us more about the query and the object on which the plan is generated.

You can also right-click any operator or arrow in the plan and select Properties to learn the more detailed explanation of the metrics displayed in the tooltip. 

 

Conclusion

In this article, we have learned what execution plans in SQL Server are and how to generate one. We also walked through various metrics that are being considered in the operators used in the plan. Finally, we have seen how to save an execution plan in SQL Server Management Studio to the file system and use it for future references. 

 

标签:Execution,Plans,being,Estimated,Server,plan,query,execution
来源: https://www.cnblogs.com/chucklu/p/14823487.html