SQL-Server-First-Responder-Kit icon indicating copy to clipboard operation
SQL-Server-First-Responder-Kit copied to clipboard

sp_BlitzCache could detect guided plans

Open LitKnd opened this issue 12 years ago • 3 comments

I don't think this is super high priority. Or even high priority. But it looks like it'd be easy.

In an execution plan just look for the presence of "PlanGuideName"

The following repro works on SQL14 for StackOverflow. If you needed a repro for adventureworks2012 I could put something together.

DBCC FREEPROCCACHE
GO
use StackOverflow;
GO

IF OBJECT_ID('dbo.AcceptedAnswersByUser') is null
    exec ('create procedure dbo.AcceptedAnswersByUser as return 0')
GO
ALTER PROCEDURE dbo.AcceptedAnswersByUser
    @UserId INT
AS
SELECT 
    (CAST(Count(a.Id) AS float) / (SELECT Count(*) FROM Posts WHERE OwnerUserId = @UserId AND PostTypeId = 2) * 100) AS AcceptedPercentage
FROM dbo.Posts q
JOIN dbo.Posts a ON q.AcceptedAnswerId = a.Id
WHERE
    a.OwnerUserId = @UserId
  AND
    a.PostTypeId = 2
GO


EXEC sp_create_plan_guide 
    @name = N'AcceptedAnswersByUser-Optimize For Value', 
    @stmt = N'SELECT 
    (CAST(Count(a.Id) AS float) / (SELECT Count(*) FROM Posts WHERE OwnerUserId = @UserId AND PostTypeId = 2) * 100) AS AcceptedPercentage
FROM dbo.Posts q
JOIN dbo.Posts a ON q.AcceptedAnswerId = a.Id
WHERE
    a.OwnerUserId = @UserId
  AND
    a.PostTypeId = 2', 
    @type = N'OBJECT', 
    @module_or_batch = N'[dbo].[AcceptedAnswersByUser]', 
    @hints = N'OPTION (OPTIMIZE FOR (@UserId=557499))'
GO

exec dbo.AcceptedAnswersByUser 22656;
GO

EXEC sp_control_plan_guide 'Drop', 'AcceptedAnswersByUser-Optimize For Value';
GO

LitKnd avatar Jun 18 '14 01:06 LitKnd

While an AdventureWorks2012_CS repro would be fantastic, but if there's a StackOverflow up in der cloud, I can just use that.

peschkaj avatar Jun 18 '14 01:06 peschkaj

Here is AdventureWorks2012 Repro

Use AdventureWorks2012
GO

CREATE PROCEDURE dbo.GuideMe
AS
    SELECT City
    FROM Person.Address AS a
    WHERE StateProvinceID=79
GO


/* Create the plan guide with an index that exists */
EXEC sp_create_plan_guide 
    @name = N'Plan Guide Test', 
    @stmt = N'SELECT City
    FROM Person.Address AS a
    WHERE StateProvinceID=79', 
    @type = N'OBJECT', 
    @module_or_batch = N'dbo.GuideMe', 
    @hints = N'OPTION (TABLE HINT ( a, INDEX ([IX_Address_StateProvinceID]) ) )'
GO

/* This should show as a guided plan */
EXEC dbo.GuideMe;
GO

/* Cleanup plan guide */
EXEC sp_control_plan_guide 'Drop', 'Plan Guide Test';
GO

DROP PROCEDURE dbo.GuideMe

LitKnd avatar Jun 18 '14 02:06 LitKnd

This issue was moved to BrentOzarULTD/sp_BlitzCache#8

peschkaj avatar May 12 '15 16:05 peschkaj