The other two answers here (@KM. And @gbn) list the reasons why SQL "invalidates" the cached plan.
What I mean by "cancellation" is that the existing plan required a recompilation.
However, I believe the reason for # 1 recompilation is that the cached plan no longer exists.
This may be due to:
- Memory pressure on the cache to make room for newly created plans.
Maybe you have a ton of random / prepared scripts that work, and that leaves little room for plans created for your stored procedures - Server rebooted / rebooted
This will clear your tempdb and cache. - Someone set up routine work to periodically clear the cache
(because they donβt know what the hell they are doing)
Here is a great link to # 3 above (how clearing the cache can be done manually):
My link to the link above was to look for your jobs and stored procedures for anything containing:
- "DBCC FREE"
- "DBCC FLUSH"
- "CLEAR PROC"
I worked in various databases, and I was lucky to see that my cached stored procedure plans last no more than 24 hours (usually much less), so the next day everything should be recompiled.
In my experience, caching works, but usually only within a few hours of a business day, so never expect your plans to go beyond this.
source share