![]() |
|
|
By: Tibor Nagy | Read Comments (6) | Print Tibor Nagy is a technical and business professional from the financial industry. His experience includes SQL Server 2000-2008, DB2 and MySQL. Related Tips: More |
|
I have been using Graphical Query Plans for some time, but I know that Text Plans are also available. I have looked at them a few times, but I don't really understand how to relate what I know about the Graphical Plans to the Text Plan format.
In this tip we will take a look at a sample query and how the Graphical Plans maps to the Text Plan output.
There are two different types of query plans, the estimated and the actual execution plan. You can obtain these plans in three formats: text, graphical and XML. Note that if you try to view an execution plan and have issues you may need to have the SHOWPLAN permission granted to your login. You can read more about SHOWPLAN security on MSDN.
Generally Graphical Plans are easier to read for beginners and also for query plans that are not overly complex. When the query plan gets more complex Text Plans are generally a better option although they can be overwhelming at first sight.
Text Plans maybe harder to read, but they contain more information without the need to check the properties for each operation. For Graphical Plans the details for each operation can be displayed using the ToolTips by hovering your mouse over the operation or using the properties window. When you hover over an operation or click on the operation a list of operator properties is displayed, but unfortunately it can be difficult to check all the details for a complex query using this tool.
Let's check the Graphical and the Text Execution Plans for a query in the AdventureWorksDW2008R2 database and compare the output.
You can display the Estimated Execution Plan in SQL Management Studio by pressing CTRL + L in the query window or CTRL + M for the Actual Execution Plan.
For this example we will use the Estimated Execution Plan and then run the following query.
SELECT * FROM dbo.DimOrganization LEFT JOIN dbo.DimCurrency ON DimOrganization.CurrencyKey=DimCurrency.CurrencyKey WHERE DimOrganization.ParentOrganizationKey IS NOT NULL ORDER BY DimOrganization.ParentOrganizationKey,DimOrganization.CurrencyKey
You should read a Graphical Plan from right to left and top to bottom following the arrow between the icons. I have added red numbers to show the sequence of events below.

On the Graphical Plan you can check that the execution order by following the arrows from right-to-left:
The data passed between operations is represented by the thickness of the arrows on the Graphical Plan and if you hover over the arrow you can see the Estimated Number of Rows that are passed between operations.
There are three ways to obtain text plans:
To turn these on you use the ON option and turn off you use the OFF option as shown below.
Since we want to get the estimated execution plan with details to match the graphical plan we will run the following code:
SET SHOWPLAN_ALL ON GO SELECT * FROM dbo.DimOrganization LEFT JOIN dbo.DimCurrency ON DimOrganization.CurrencyKey=DimCurrency.CurrencyKey WHERE DimOrganization.ParentOrganizationKey IS NOT NULL ORDER BY DimOrganization.ParentOrganizationKey,DimOrganization.CurrencyKey GO SET SHOWPLAN_ALL OFF GO
The Text Plan for the query is below and I broke this up into three screenshots to show all the details.
When you interpret a Text Plan, you should look for the pipes (|) which connect the child and parent nodes. You can see these in the StmtText column. Each indentation is another level deeper. Note that I added red numbers to show the order of operations and these numbers correspond to the numbers above in the Graphical Query Plan.



The Text Plan contains the execution order through the NodeId and Parent columns. The Parent column shows which operation will use the output of the current operator. You should start with a statement where the StmtText starts at the rightmost position behind a pipe character. For this query it is the Clustered Index Scan (NodeId 4).
On the Text Plans you can easily see all of the detailed properties which are shown in Graphical Query Plan ToolTips. One such item is the EstimateRows which shows the estimated number of rows passed between operations.
I hope this helps you see that Text Plans are not that hard to use and as your query plans get more and more complex that this might be a better option to tune your queries.
| Share: | Share | Tweet |
|
![]() |
![]() |
Connect with MSSQLTips.com |
| Tuesday, January 10, 2012 - 10:11:40 AM - David | Read The Tip |
|
Very useful tip. I was unaware ofthe text option you presented in the article. I could not find the DimOrganization table so I used some queries for Adventureworks. USE ADVENTUREWORKS GO SET SHOWPLAN_ALL ON GO SELECT * FROM Sales.SalesOrderDetail s INNER JOIN Production.Product p ON s.ProductID = p.ProductID GO SELECT * FROM Production.TransactionHistory th INNER JOIN Production.TransactionHistoryArchive tha ON th.Quantity = tha.Quantity GO SET SHOWPLAN_ALL OFF GO |
|
| Tuesday, January 10, 2012 - 10:43:27 AM - Tibor Nagy | Read The Tip |
|
Hi David,
You are using AdventureWorks database this why you cannot find the DimOrganization table. In the example I used AdventureWorksDW2008R2 database which can be downloaded from Codeplex site for SQL Server 2008 R2. Anyway, it was just an example to show how to compare the query plans. You can experiment with any query plan.
Regards, Tibor |
|
| Tuesday, January 10, 2012 - 11:29:53 AM - Scott C | Read The Tip |
|
This was a useful tip overall, but your interpretation of the sequence of operations is inaccurate. The sort operation does not run in parallel with the clustered index seek. Most sorts are blocking operations, all input rows are spooled to a temp table before continuing to the next step. In this case the whole point of the sort operation is to get all the CurrencyKey values from the index scan in the same order as the clustered primary key, before doing the first seek. Then the clustered index seek operation is more like a merge, it only requires one pass through the table rather than jumping around at random. |
|
| Tuesday, January 10, 2012 - 11:34:50 AM - Scott C | Read The Tip |
|
I didn't read the query closely enough, the sort is by ParentOrganizationKey, CurrencyKey and not by CurrencyKey alone. But the point is that the sort will finish before the seeks begin, otherwise the sort operation would be to the left of the Nested Loop join. |
|
| Tuesday, January 10, 2012 - 1:40:47 PM - Tibor Nagy | Read The Tip |
|
Hi Scott, Actually, when I wrote that they run in paralel, my intention was to show that the operators both finish before the operator on the left. It is just generalization, the key is to show junior DBAs how to use text plans if they are familiar with graphical plans. Physically they can run in paralel or not, but the point is that they both should finish before the operator on the left. |
|
| Friday, March 16, 2012 - 4:41:56 AM - Dick Baker | Read The Tip |
|
suggest you advise readers that the SET SHOWPLAN_TEXT option is now deprecated, so folk should concentrate on Graphic/XML formats |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |