Sql-server – Is it possible to get the name of a procedure by it’s plan handle or query plan hash if the execution plan is already cleared from the plan cache

dmvplan-cachequery-cachesql serversql-server-2016

I ran a home-brew trace for the length of 1 day that captured a few things that were running. The trace includes the specific query text, the parent entity text of that query, the Plan Handle, and the Query Plan Hash.

Usually I can use RedGate's SQL Search on the query text to find the entity I'm looking for but that was a no-go in this case.
Unfortunately that was a day ago when the trace ran, and when I check the sys.dm_exec_cached_plans DMV, nothing is turning up for my plan handle.

Are there any other ways I can backtrace this query?

Update: Here's how the Query Store is currently configured on the database in question. Note sure if it can be useful for my case?

enter image description here

Best Answer

If (and that's a big IF) you're running SQL Server 2019 (CTP 2.4) then you can get the last known ACTUAL execution plan for a previously cached query plan:

SELECT qps.query_plan
FROM   sys.dm_exec_procedure_stats AS ps
       CROSS APPLY sys.dm_exec_query_plan_stats(deps.plan_handle) AS qps
WHERE  ps.object_id = OBJECT_ID('dbo.spMyProcedure');