SQL Server 2008 - Worth the Wait
One of the biggest frustrations for a DBA is performance tuning third-party applications. In most cases you can't modify the code directly, so you have to live with the code the application provides you. In most cases, your only opportunity for tuning third-party applications is to add or modify indexes. Sometimes, this can really help out performance.
But what happens if you have added all the appropriate indexes and discover that there are a couple of really bad performing queries that can't be helped by indexing? One option is to complain to the application's vendor. They might actually listen and fix the problem in the next patch. But most likely they won't, and you will be blamed for a problem you can't fix, even though you know exactly what the problem is.
In SQL Server 2005, there is a new feature called Plan Guides that can help out in some cases where you discover poorly performing queries that you don't have direct control over. Essentially, a Plan Guide allows you to add or modify query hints to queries on the fly, just before they are executed.
Here's how they work:
I am not a personal fan of using query hints to modify queries, but in some cases they are necessary to fix badly behaving queries. Plan Guides offer an opportunity to "fix" some poorly performing queries that you aren't able to directly touch and fix yourself. Of course, the assumption here is that you are an experienced DBA and know enough to recognize when a hint is appropriate or not appropriate for a particular query.
Plan Guides can be applied to queries in three different situations:
Although it would be rare to use many of the hints, the following hints can be included in a Plan Guide. Any combination of valid query hints is fine.
There are two stored procedures used to create and manage Plan Guides:
Let's take a brief look at each.
As you can probably guess, sp_create_plan_guide is used to create new Plan Guides, using the following syntax.
sp_create_plan_guide [ @name = ] N'plan_guide_name' , [ @stmt = ] N'statement_text' , [ @type = ] N'{ OBJECT | SQL | TEMPLATE }' , [ @module_or_batch = ] { N'[ schema_name. ] object_name' | N'batch_text' | NULL } , [ @params = ] { N'@parameter_name data_type [ ,...n ]' | NULL } , [ @hints = ] { N'OPTION ( query_hint [ ,...n ] )' | NULL }
Since syntax examples, like the one above, are a little hard to follow, let's look at a real example.
sp_create_plan_guide @name = N'PlanGuide1', @stmt = N'SELECT COUNT(*) AS Total FROM Sales.SalesOrderHeader h, Sales.SalesOrderDetail d WHERE h.SalesOrderID = d.SalesOrderID and h.OrderDate BETWEEN "1/1/2000" AND "1/1/2005" ', @type = N'SQL', @module_or_batch = NULL, @params = NULL, @hints = N'OPTION (MERGE JOIN)' GO
As I mentioned earlier, there are three kinds of Plan Guides. The example above is a SQL Plan Guide, which is used to identify a particular string of code. Once the code is identified and looked up in the Plan Guide lookup table, the appropriate hint is added to the query for compilation and execution.