DATABASE

SQL Server 2008 : Managing Query Performance - Adding Hints Through Plan Guides

2/19/2011 4:03:37 PM
Plan guides were introduced in SQL Server 2005 to provide the ability to add a hint to a query without having to manually alter the query. Many times, you are at the mercy of the queries thrown at your database by certain applications. Plan guides let you supply a hint to those incoming application queries in order to produce a more efficient query plan.

SQL Server 2008 has actually taken plan guides a step further so that you can provide an entire XML query plan as a query hint. Unlike the USE PLAN query hint, an invalid XML query plan supplied as a query hint using plan guides will not cause the query to fail.

1. Creating a Plan Guide

To create a new plan guide, expand the Programmability node under the database where you would like the plan guide, right-click the Plan Guides folder, and select New Plan Guide from the context menu to display the New Plan Guide dialog box, as shown in Figure 1.

Figure 1. New Plan Guide dialog box

Let's create a plan guide for the same query used in Listing 1 of this article to force the execution plan to use an index seek instead of an index scan. The options available when creating a new plan guide include the following:

  • Name: Enter a descriptive name for the plan guide. We named ours PlanGuide-Ch15Test.

  • Statement: Enter the T-SQL statement for which you would like to apply the plan guide. We entered the T-SQL statement from Listing 1 of this article.

Make sure you enter the T-SQL statement exactly as it will be sent to SQL Server. Even extra spaces will cause the query optimizer to ignore the plan guide.


  • Scope Type: The scope type specifies the context for matching the T-SQL statement to the plan guide. Valid values are Object, Sql, and Template. Object is the stored procedure, scalar function, multi-statement table-valued function, or T-SQL Data Manipulation Language (DML) trigger that contains the T-SQL statement. Sql indicates that the T-SQL statement is in the context of a stand-alone statement or batch. Template is used to change the default query parameterization behavior. If Template is selected, you can only use the PARAMETERIZATION {FORCED | SIMPLE} query hint. We will be using the Sql scope type in this example.

  • Scope Batch: Enter the T-SQL batch that contains the statement for which you would like to apply the plan guide. If you leave the Scope Batch box blank, the Statement text will be used.

  • Scope Schema Name: If the scope type is Object, enter the name of the schema that contains the object.

  • Scope Object Name: If the scope type is Object, enter the name of the object that contains the T-SQL statement for which you would like to apply the plan guide.

  • Parameters: Enter the parameter name and data type for the T-SQL statement. The parameters must be submitted exactly as they appear using the sp_executesql statement. If the scope type is Template, parameters are required.

  • Hints: Enter the query hint or XML execution plan that will be applied to the T-SQL statement. SQL Server 2008 allows you to use the FORCESEEK table hint as a query hint by entering the following statement. (You should also notice that we are referencing the alias B for the table name we used in the query instead of the actual table name.)

    OPTION (TABLE HINT (B, FORCESEEK))

The final T-SQL command to create the plan guide is shown in Listing 1. You can query the sys.plan_guides catalog view to return information for each plan guide you have created in the current database.

Example 1. T-SQL Code to Create the Plan Guide Shown in Figure 1
USE AdventureWorks2008
GO

EXEC sp_create_plan_guide
@name = N'[PlanGuide-Ch15Test]',
@stmt = N'Select *
FROM HumanResources.Employee A
JOIN HumanResources.EmployeeDepartmentHistory B
ON A.BusinessEntityID = B.BusinessEntityID
WHERE A.BusinessEntityID > 270 OR
(A.BusinessEntityID < 10 and B.DepartmentID =1)',
@type = N'SQL',
@hints = N'OPTION (TABLE HINT(B, FORCESEEK))'
GO

2. Validating a Plan Guide

Because a plan guide will not cause a query to fail, it is important to validate the plan guides. This is primarily necessary after you perform an upgrade to SQL Server or change the underlying data structure in the database. If the plan guide is invalid, the query optimizer will ignore it and generate a new execution plan for the query. You can use the sys.fn_validate_plan_guide function to validate a plan guide by passing the plan_guide_id function from the sys.plan_guides catalog view. The sys.fn_validate_plan_guide function will return the first error message encountered when the plan guide is applied to the query. If you receive an empty result set, the plan guide does not contain any errors. The following script will check the validity of all the plan guides in the AdventureWorks2008 database.

USE AdventureWorks2008
GO

SELECT plan_guide_id, msgnum, severity, state, message
FROM sys.plan_guides
CROSS APPLY sys.fn_validate_plan_guide(plan_guide_id)

You can use the Plan Guide Successful and Plan Guide Unsuccessful events in SQL Server Profiler located under the Performance node to capture valid and invalid attempts of the query optimizer using plan guides. Start a new Profiler trace and select Show All Events, expand the Performance node, and select Plan Guide Successful and Plan Guide Unsuccessful, as shown in Figure 2. Select Run to start the trace.

Figure 2. Plan guide events in SQL Server Profiler

Now, execute the query from Listing 1 of this article that we used to create the plan guide. As you can see in Figure 3, the query optimizer successfully applied the plan guide to the query, and then the query was executed using the hint provided in the plan guide.

Figure 3. Successful plan guide execution captured using SQL Server Profiler

Other  
  •  SQL Server 2008 : Managing Query Performance - Forcing a Specific Execution Plan
  •  Programming Microsoft SQL Server 2005 : An Overview of SQL CLR - CLR Triggers
  •  Programming Microsoft SQL Server 2005 : An Overview of SQL CLR - CLR Functions
  •  SQL Server 2008 : Performance Tuning - Using Dynamic Management Views
  •  SQL Server 2008 : Performance Tuning - Working with Database Tuning Advisor
  •  SQL Server 2008 : Performance Tuning - Tracing with SQL Profiler
  •  SQL Server 2008 : Performance Tuning - Working with Query Execution Plans
  •  Externalizing BLOB Storage in SharePoint 2010 (part 2) - Installing and Configuring RBS & Migrating and Moving BLOBs Between BLOB Stores
  •  Externalizing BLOB Storage in SharePoint 2010 (part 1)
  •  Sharepoint 2010 : Managing SharePoint Content Databases
  •  Sharepoint 2010 : Maintaining SQL Server in a SharePoint Environment
  •  Sharepoint 2010 : Monitoring SQL Server in a SharePoint Environment
  •  Database Availability Group Replication in Exchange Server 2010 : Load Balancing in Exchange Server 2010
  •  Database Availability Group Replication in Exchange Server 2010 : Comparing and Contrasting DAG Versus CCR/SCR/SCC
  •  SQL Server 2008: Managing Query Performance - Forcing Index Seeks
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 4)
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 3)
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 2) - Suspending and Reseeding a Database
  •  Exchange Server 2010 : Deploying a Database Availability Group (part 1) - Creating the File Share Witness
  •  Database Availability Group Replication in Exchange Server 2010 : Understanding Database Availability Groups
  •  
    Most View
    Programming COM+ Security (part 3) - Compiling and Installing the COM+ Application
    Acoustic Energy 301 Loudspeaker - Metal Master (Part 1)
    The Revolution Of Visual Resolution (Part 4) - ViewSonic VX2336S-LED, AG Neovo L-W27, Hannsg HL272HPB
    Programming .NET Security : Programming Cryptographic Keys (part 1) - Creating Keys
    Java EE 6 with GlassFish 3 Application Server : JSP implicit objects
    Big Onkyo Is A Power - Packed Heavyweight
    Home Cinema, April-2012 (Part 2) - Philips 7000 series Smart LED TV & Toshiba Regza 46WL863B
    Get Started With iPhoto For iOS (Part 2)
    Collaborating via Social Networks and Groupware : Creating Groups on Social Networks
    Stylish Loewe 3D TV
    Top 10
    Western Digital Sentinel DX4000 NAS Review (Part 4)
    Western Digital Sentinel DX4000 NAS Review (Part 3)
    Western Digital Sentinel DX4000 NAS Review (Part 2)
    Western Digital Sentinel DX4000 NAS Review (Part 1)
    Enlightenment E17 - Eauty At Your Fingertips
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 3)
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 2)
    Photo Editors: From Professional RAW Tools To Simple Library Management (Part 1)
    Getting The Most From Passbook
    HTC One SV - The Best Bits Of The Impressive One Series