Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Doing UPSERTs in MongoDBGiving users the ability to change a stored procedure without making them db_owner »
    comments

    Another document for the customer's DBA checklist. This time we are going to look how we find and read an Execution Plan from a query.

    Showing an Execution Plan
    I will only focus on the graphical Execution Plans and in the examples the AdventureWorks2008R2 OLTP database is used which you can download here.
    To see a Query Execution plan, you need to create a query:

    1. SELECT * FROM Sales.vStoreWithContacts

    And then you click the "Display Estimated Execution Plan" or the "Include Actual Execution Plan" button in the SQL Editor Toolbar of SQL Server Management Studio.

    The difference between the Estimated and the Actual plan is the execution of the query itself. When you hit the "Display Estimated Execution Plan" button, SQL Server Management Studio will immediately show you an Execution plan, however it's an estimated plan and the query itself is NOT executed. When you hit the "Include Actual Execution Plan" nothing will happen until you execute the query. After execution of the query a third tab will appear in SSMS showing the Execution Plan that was actually used:

    Reading Execution Plans
    When you want to read the above Execution Plan you have to read from right to left and from bottom to top. So SQL Server will start with

    And end with

    Other information you directly can see is the most expensive operation and based on the thickness of the connecting arrows you can see the amount of data that�s streaming through them:

    Finding more information
    When you hover your mouse over the arrows or operations in an Execution Plan a pop-up will appear giving you more detailed information about the rows or operation:

    Needless to say that the Actual Number of Rows and some other information will not be available in an Estimated Execution Plan. In the example above we see a big difference between the Estimated and the Actual Number of Rows which might indicate that our statistics are out of date.
    For even more information select an operation and open the Properties Window (F4):

    Another option to get more out of your Execution Plans is to download SQL Sentry Plan Explorer more information is found at their site but here is a screenshot from the same Execution Plan in the Free Plan Explorer:

    As you can see, the most expensive operation is highlighted in red and the tool also highlights the difference between the estimated and actual rows.

    Missing index information
    When executing the following query:

    1. SELECT * FROM Sales.vStoreWithContacts
    2. WHERE Firstname = 'Alan'

    You’ll see some green text in the top of the Execution Plan:

    This information comes from the Dynamic Management View: sys.dm_db_missing_index_details. More information on Indexes is out of scope for this post but be careful with just implanting these Missing Indexes. They might improve the performance of this query but since they need to be maintained they can also slow down other operations on the database.

    Comparing Execution Plans
    Are you rewriting a query and wondering which one should perform better? You can easily compare the Execution Plans of two queries. Just write these queries in one query window:

    1. SELECT * FROM Sales.vStoreWithContacts
    2. WHERE Firstname NOT LIKE 'A%';
    3.  
    4. select * FROM Sales.vStoreWithContacts
    5. WHERE FirstName IN (SELECT FirstName FROM Sales.vStoreWithContacts
    6. WHERE FirstName NOT LIKE 'A%');
    7. GO

    When you execute the queries you’ll see they both return the same 70 rows but when you look at the header of the Execution Plans you’ll see that the first query costs less than the second one in this batch:

    Conclusion
    Execution Plans gives us valuable information and a deep insight in how SQL Server executes the queries we write. Reading and understanding Execution Plans are essential for writing well performing queries.

    About the Author

    User bio imageAxel started his IT career more than a decade ago. After a few months he was the accidental DBA for the SQL Server 6.5 running the Service Desk application. It didn't took long or Axel started to focus on SQL Server and after another year he became the full-time DBA in a large Belgian datacenter managing the SQL Servers for the hosted customers.It's more than five years now since he switched to consulting and teaching SQL Server. Axel is a Microsoft Certified DBA and Trainer.
    Social SitingsTwitterLinkedInLTD RSS Feed
    2286 views
    InstapaperVote on HN

    4 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis Nice post, I also like to look at plans in text form

    for example

    SET SHOWPLAN_TEXT ON
    go

    SELECT name as ConstraintName ,
    OBJECT_NAME(parent_object_id) as TableName
    FROM sys.foreign_keys
    go

    SET SHOWPLAN_TEXT OFF
    GO


    Here is the plan in text form
    |--Filter(WHERE:(has_access('CO',[master].[sys].[sysschobjs].[id] as [o].[id])=(1)))
    |--Compute Scalar(DEFINE:([Expr1048]=object_name([master].[sys].[sysschobjs].[pid] as [o].[pid])))
    |--Clustered Index Scan(OBJECT:([master].[sys].[sysschobjs].[clst] AS [o]),
    WHERE:([master].[sys].[sysschobjs].[nsclass] as [o].[nsclass]=(0)
    AND [master].[sys].[sysschobjs].[pclass] as [o].[pclass]=(1)
    AND [master].[sys].[sysschobjs].[type] as [o].[type]='F '))

    And yes, I like that you added SQL Sentry's Plan Explorer to this post, it does much more than the regular SSMS execution plan
    01/10/13 @ 04:22
    Comment from: Axel Achten (axel8s) [Member]
    Axel Achten (axel8s) Thanks, I didn't include the SHOWPLAN_TEXT because it is announced to be deprecated in the next release of SQL Server and I don't like XML :D
    01/10/13 @ 05:11
    Comment from: SQLDenis [Member] Email
    SQLDenis Ha, learn something everyday, I did not know that these were going to be deprecated

    SET SHOWPLAN_TEXT
    SET SHOWPLAN_ALL
    SET STATISTICS PROFILE

    These are to be replaced with

    SET SHOWPLAN_XML (TEXT and ALL)
    SET STATISTICS XML
    01/10/13 @ 05:21
    Comment from: Axel Achten (axel8s) [Member]
    Axel Achten (axel8s) At your service ;)
    01/10/13 @ 05:25

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)