- Learn T-SQL Querying
- Pedro Lopes Pam Lahoud
- 604字
- 2021-06-24 14:38:18
Accessing a query plan
To access the estimated plans, which are the direct result of the optimization process, we can use either T-SQL commands or graphical tools. For the examples shown in this chapter, we use SQL Server Management Studio (SSMS).
The SHOWPLAN_TEXT, SHOWPLAN_ALL, and SHOWPLAN_XML commands provide text-based information on query plans with different degrees of detail. Using any of these commands means SQL Server will not execute the T-SQL statements, but show the query plan as produced by the Query Optimizer.
Take an example of a query that can be executed in the scope of the AdventureWorks sample database:
SELECT pp.FirstName, pp.LastName, pa.AddressLine1, pa.City, pa.PostalCode
FROM Person.Address AS pa
INNER JOIN Person.BusinessEntityAddress AS pbea ON pa.AddressID = pbea.AddressID
INNER JOIN Person.Person AS pp ON pbea.BusinessEntityID = pp.BusinessEntityID
WHERE pa.AddressID = 100;
Let's see what each of the following options provides in terms of query plan view:
- SHOWPLAN_TEXT: This option shows all the steps involved in processing the query, including the type of join that was used, the order in which tables are accessed, and the indexes used for each table:

- SHOWPLAN_ALL: This option shows the same estimated plan as SHOWPLAN_TEXT. This option represents a text output tree, but adds details on each of the physical operations that would be executed, such as the estimated size of the result rows, the estimated CPU time, and the total cost estimations. Notice the amount of information produced:

- SHOWPLAN_XML: This option produces the same estimated plan but as an XML output tree:

Because it is generated as a link when used in SSMS, it can be interpreted by SSMS as a graphical estimated plan, and clicking the link will display this graphical plan:

Notice that because it is an estimated plan, the arrows are all the same width. This is because there's no actual data movement between operators given that this plan was not executed. To access all the properties returned by SHOWPLAN_ALL, plus many more, right-click the SELECT operator and click on Properties. We will see these properties in greater detail in the Query plan properties of interest section.
SHOWPLAN_XML is the option is used by SSMS when the Display Estimated Execution Plan (CTRL+L) button is clicked:

To access the actual plans, which are the optimized plans after being executed, we can again use either T-SQL commands or graphical tools. The STATISTICS PROFILE and STATISTICS XML commands provide text-based information on query plans with different degrees of detail. Using either of these commands means SQL Server will execute the T-SQL statements, and generate the actual plan, or query execution plan.
- STATISTICS PROFILE: This option shows the same plan as SHOWPLAN_ALL, incremented with the actual rows and executes to display an actual plan, or a query execution plan:

- STATISTICS XML: This option is the actual plan counterpart of SHOWPLAN_XML. In the following screenshot, we see what appears to be the same output as SHOWPLAN_XML:

However, expanding the XML (or if using SSMS, clicking on the link), we see we have the actual plan, or the query execution plan:

STATISTICS XML is the option used by SSMS when the Display Actual Execution Plan (CTRL+M) button is clicked:

To access all the properties already seen with SHOWPLAN_XML incremented with runtime statistics and warnings (if any), right-click the SELECT operator and click on Properties. Again, we will see these properties in greater detail in the Query plan properties of interest section.
- 零起步輕松學單片機技術(第2版)
- 大數據管理系統
- JavaScript實例自學手冊
- MCSA Windows Server 2016 Certification Guide:Exam 70-741
- PyTorch深度學習實戰
- Python Algorithmic Trading Cookbook
- Grome Terrain Modeling with Ogre3D,UDK,and Unity3D
- 水下無線傳感器網絡的通信與決策技術
- RedHat Linux用戶基礎
- 教育機器人的風口:全球發展現狀及趨勢
- 液壓機智能故障診斷方法集成技術
- 人工智能:智能人機交互
- 貫通Hibernate開發
- Machine Learning with Spark(Second Edition)
- 玩機器人 學單片機