Can a plan "age" reach zero?
My question is when could the Age of a plan reach zero?
The algorithm that SQL Server uses to determine when and how plans should be removed from cache is called the eviction policy.
The cost of plan is analyzed to determine which plans gets evicted. Upon detecting memory pressure, zero cost plans are removed from the cache and the cost of all other plans is reduced by half.
- For adhoc plans, the cost is considered to be zero, but it is increased by one every time the plan is reused.
- For other types of plans, the cost is a measure of the resources required to produce the plan. When one of these plans is reused, the cost is reset to the original cost.
- For non–adhoc queries, the cost is measured in units called ticks, with a maximum of 31. The cost is based on three factors: I/O, context switches, and memory. Each has its own maximum within the 31-tick total.
When not under memory pressure, costs are not decreased until the total size of all plans cached reaches 50 percent of the buffer pool size. At that point, the next plan access will decrement the cost in ticks of all plans by 1.
Once memory pressure is encountered, then SQL Server will start a dedicated resource monitor thread to decrement the cost of either plan objects in one particular cache (for local pressure) or all plan cache objects (for global pressure).
so the gist is ...
The Clock algorithm sweeps the cache at regular intervals. Every time an unused entry is found, the cost is decreased by some amount. If the cost is 0 and is not used, then it is removed from the cache.
Best references :
- Plan Cache Internals
- An in-depth look at SQL Server Memory–Part 1 and Part 2