Skip to content

New Plans not deducted due to same plan_handle for different plans of same query #80

@Mahadevannkl

Description

@Mahadevannkl

I've noticed below issues.

I've a SP which has 2 different query plans on 2 different execution after flushing cache one after another. But, it has same plan_handle though different physical plan(xml).
Hence OQS not capturing the second xml plan as it has same plan_handle.

I think plan_query_hash is unique rather than plan_handle.

Here is the code to reproduce:

use tempdb

go
IF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL
DROP TABLE dbo.Customers
GO

CREATE TABLE Customers
(
CustomerID INT NOT NULL IDENTITY(1,1) ,
CustomerName VARCHAR(50) NOT NULL ,
CustomerAddress VARCHAR(50) NOT NULL ,
[State] CHAR(2) NOT NULL ,
CustomerCategoryID CHAR(1) NOT NULL ,
LastBuyDate DATETIME ,
PRIMARY KEY CLUSTERED ( CustomerID )
)

IF OBJECT_ID('dbo.CustomerCategory', 'U') IS NOT NULL
DROP TABLE dbo.CustomerCategory
GO

CREATE TABLE CustomerCategory
(
CustomerCategoryID CHAR(1) NOT NULL ,
CategoryDescription VARCHAR(50) NOT NULL ,
PRIMARY KEY CLUSTERED ( CustomerCategoryID )
)

CREATE INDEX IX_Customers_CustomerCategoryID
ON Customers(CustomerCategoryID)

GO

INSERT INTO [dbo].[Customers] (
[CustomerName],
[CustomerAddress],
[State],
[CustomerCategoryID],
[LastBuyDate])
SELECT
'Desiree Lambert',
'271 Fabien Parkway',
'NY',
'B',
'2013-01-13 21:44:21'

GO

INSERT INTO [dbo].[Customers] (
[CustomerName],
[CustomerAddress],
[State],
[CustomerCategoryID],
[LastBuyDate])
SELECT
'Pablo Terry',
'29 West Milton St.',
'DE',
'A',
GETDATE()

go 15000

GO

CREATE PROCEDURE Test_Sniffing
@CustomerCategoryID CHAR(1)
AS

SELECT C.CustomerName,
C.LastBuyDate
FROM dbo.Customers C
INNER JOIN dbo.CustomerCategory CC ON CC.CustomerCategoryID = C.CustomerCategoryID
WHERE CC.CustomerCategoryID = @CustomerCategoryID

GO

DBCC FREEPROCCACHE()
GO

DECLARE @CustomerCategoryID CHAR(1)

SET @CustomerCategoryID = 'A'
EXEC dbo.Test_Sniffing @CustomerCategoryID
GO

---Plan 1
select sql_handle,plan_handle,query_plan From sys.dm_exec_procedure_stats cross apply sys.dm_exec_sql_text(sql_handle)
cross apply sys.dm_exec_query_plan(plan_handle)
where text like '%Test_Sniffing%'
go

DBCC FREEPROCCACHE()
GO

DECLARE @CustomerCategoryID CHAR(1)

SET @CustomerCategoryID = 'B'
EXEC dbo.Test_Sniffing @CustomerCategoryID
GO
-----Plan2 - Same plan_handle as plan 1. But physical plan is different(xml)
select sql_handle,plan_handle,query_plan From sys.dm_exec_procedure_stats cross apply sys.dm_exec_sql_text(sql_handle)
cross apply sys.dm_exec_query_plan(plan_handle)
where text like '%Test_Sniffing%'
go

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions