Sql-server – Why does the plan cache contain estimated and not actual execution plans

execution-planplan-cachesql server

I can think of many reasons behind this decision of storing estimated plans in the plan cache and not the actual plan. But I can't find the "correct" answer.

Best Answer

Think about what "actual" means. It's what actually happens for the execution of that plan.

Another common name for the actual execution plan is the "post execution plan". As a real world example to correlate this scenario, say you plan to go on a cross country trip, so you plot out the roads you're going to take and how long you think it'll take. But because of road work and detours, it doesn't happen quite like you planned. That's the actual execution plan: The actual route that you took to get to where you're going. The estimated plan would have been what you thought you'd be doing, and what route you thought you should take.

You can't store post execution plans because they are just that....post execution. They contain metrics of what really happened, not what SQL Server thinks should happen (estimated execution plan).

The plan cache is really just a repository of "maps" that it can reuse without having to spend the expensive time to draw up a new one. Actual execution plans are after the fact, and the plan cache is to handle before the fact.