How to clear cache of 1 stored procedure in sql server
just find the plan using this query and clean the plan_handle
SELECT [text], cp.size_in_bytes, plan_handle
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE cp.cacheobjtype = N'Compiled Plan'
AND cp.objtype = N'Adhoc'
AND cp.usecounts = 1
ORDER BY cp.size_in_bytes DESC;
DBCCFREEPROCCACHE(0x0600010069AB592540C10089000000000000000000000000)
Plan_handle
Here is another way to delete the plan from the cache only for a stored procedure:
DECLARE @PlanHandle VARBINARY(64);
SELECT @PlanHandle = deps.plan_handle
FROM sys.dm_exec_procedure_stats AS deps
WHERE deps.object_id = OBJECT_ID('dbo.SomeProcedureName') AND deps.database_id = DB_ID();
IF @PlanHandle IS NOT NULL
BEGIN
DBCC FREEPROCCACHE(@PlanHandle);
END
GO
DBCC FreeProcCache
has a single optional argument - the ID of the execution plan you want to delete.
You can find the plan you want to delete using sys.dm_exec_cached_plans
, and then you can just use it as
DBCC FREEPROCCACHE (0x0123456....);
Wouldn't executing sp_recompile
on the stored procedure work?
EXEC sp_recompile N'SP_Name';