Ms Sql Server 简明教程

MS SQL Server - Execution Plans

查询优化器将在统计信息和代数/处理器树的帮助下生成执行计划。这是查询优化器的结果,并说明如何完成/执行你的工作/要求。

Execution plan will be generated by Query optimizer with the help of statistics and Algebrizer\processor tree. It is the result of Query optimizer and tells how to do\perform your work\requirement.

有两种不同的执行计划——估计的和实际的。

There are two different execution plans - Estimated and Actual.

Estimated execution plan 表示优化器视图。

Estimated execution plan indicates optimizer view.

Actual execution plan 表示执行了查询的内容以及如何执行。

Actual execution plan indicates what executed the query and how was it done.

执行计划存储在称为计划缓存的内存中,因此可以重用。除非优化器决定进行查询执行的并行性,否则每个计划只存储一次。

Execution plans are stored in memory called plan cache, hence can be reused. Each plan is stored once unless optimizer decides parallelism for the execution of the query.

SQL Server 中有三种不同的执行计划格式 - 图形计划、文本计划和 XML 计划。

There are three different formats of execution plans available in SQL Server - Graphical plans, Text plans, and XML plans.

SHOWPLAN 是希望查看执行计划的用户所需的权限。

SHOWPLAN is the permission which is required for the user who wants to see the execution plan.

Example 1

以下是查看估计执行计划的过程。

Following is the procedure to view the estimated execution plan.

Step 1 - 连接到 SQL Server 实例。在本例中,“TESTINSTANCE”是实例名称,如下面的快照所示。

Step 1 − Connect to SQL Server instance. In this case, 'TESTINSTANCE' is the instance name as shown in the following snapshot.

execution plans

Step 2 - 点击以上屏幕上的新建查询选项,并编写以下查询。在编写查询之前,选择数据库名称。在本例中,“TestDB”是数据库名称。

Step 2 − Click on New Query option on the above screen and write the following query. Before writing the query, select the database name. In this case, 'TestDB' is database name.

Select * from StudentTable
execution plans1

Step 3 - 点击上面屏幕上的红色框中标出的符号,以显示估计执行计划,如下面的截图所示。

Step 3 − Click the symbol which is highlighted in red color box on the above screen to display the estimated execution plan as shown in the following screenshot.

execution plans2

Step 4 - 将鼠标悬停在扫描表上,这是上面屏幕上红色框上方第二个符号,以查看详细的估计执行计划。出现以下截图。

Step 4 − Place the mouse on table scan which is the second symbol above the red color box in the above screen to display the estimated execution plan in detail. The following screenshot appears.

execution plans3

Example 2

以下是查看实际执行计划的过程。

Following is the procedure to view the actual execution plan.

Step 1 连接到 SQL Server 实例。在本例中,“TESTINSTANCE”是实例名称。

Step 1 Connect to SQL Server instance. In this case, 'TESTINSTANCE' is the instance name.

execution plans4

Step 2 - 点击上面屏幕上显示的新建查询按钮,并编写以下查询。在编写查询之前,选择数据库名称。在本例中,“TestDB”是数据库名称。

Step 2 − Click New Query option seen on the above screen and write the following query. Before writing the query, select the database name. In this case, 'TestDB' is database name.

Select * from StudentTable
execution plans5

Step 3 - 点击上面屏幕上红色框中标出的符号,然后执行查询以显示实际执行计划以及查询结果,如下面的截图所示。

Step 3 − Click the symbol which is highlighted in red color box on the above screen and then execute the query to display the actual execution plan along with the query result as shown in the following screenshot.

execution plans6

Step 4 - 将鼠标悬停在屏幕上红色框上方第二个符号扫描表上,以查看详细的实际执行计划。出现以下截图。

Step 4 − Place the mouse on the table scan which is the second symbol above the red color box on the screen to display the actual execution plan in detail. The following screenshot appears.

execution plans7

Step 5 - 点击上面屏幕左上角的结果以获取以下屏幕。

Step 5 − Click Results which is on the left top corner on the above screen to get the following screen.

execution plans8