<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>SQL Server internals on Rudi Bruchez</title><link>https://www.pachadata.com/en/docs/articles/sql-server/internals/</link><description>Recent content in SQL Server internals on Rudi Bruchez</description><generator>Hugo</generator><language>en</language><atom:link href="https://www.pachadata.com/en/docs/articles/sql-server/internals/index.xml" rel="self" type="application/rss+xml"/><item><title>SQL Server plan cache</title><link>https://www.pachadata.com/en/docs/articles/sql-server/internals/plan-cache/</link><pubDate>Thu, 12 Nov 2026 00:00:00 +0000</pubDate><guid>https://www.pachadata.com/en/docs/articles/sql-server/internals/plan-cache/</guid><description>&lt;p&gt;&lt;em&gt;Co-written with Claude, who ran much of the testing and research.&lt;/em&gt;&lt;/p&gt;&#10;&lt;div class="alert alert-success" role="alert"&gt;&lt;div class="h4 alert-heading" role="heading"&gt;TL;DR&lt;/div&gt;&#10;&lt;p&gt;The plan cache is the part of memory where SQL Server keeps execution plans so that it does not constantly recompile the same queries. Since SQL Server 2005, it has consisted of four stores, two of which contain plans: &lt;code&gt;SQL Plans&lt;/code&gt; for ad hoc and prepared batches, and &lt;code&gt;Object Plans&lt;/code&gt; for procedures and triggers. You can examine them with the &lt;code&gt;sys.dm_exec_cached_plans&lt;/code&gt; view.&lt;/p&gt;&#10;&lt;p&gt;An ad hoc or prepared plan is found by the exact text of its entire batch, character by character; a procedure, function, or trigger plan, by its object identifier. The engine also compares a series of attributes, the cache key, including the session&amp;rsquo;s &lt;code&gt;SET&lt;/code&gt; options and, when a name is unqualified, the user&amp;rsquo;s default schema.&lt;/p&gt;&#10;&lt;p&gt;For a plan to be parameterized and serve several values, the value must be removed from the query text. The engine does this itself through simple parameterization, enabled by default but limited to queries whose plan does not depend on the value, or through forced parameterization, which you enable per database and which parameterizes almost everything. The client does it through &lt;code&gt;sp_executesql&lt;/code&gt; with actual parameters, or by calling stored procedures. An identifier concatenated into the text, even when passed to &lt;code&gt;sp_executesql&lt;/code&gt;, produces one plan per value.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;SQL Plans&lt;/code&gt; and &lt;code&gt;Object Plans&lt;/code&gt; each have two limits: a threshold of 160,036 entries, four times the 40,009 buckets in their hash table, and a size that depends on target memory. An entry has a cost, which the cache clock decreases under pressure and which rises with each reuse.&lt;/p&gt;&#10;&lt;p&gt;A plan leaves the cache through eviction or clearing: &lt;code&gt;DBCC FREEPROCCACHE&lt;/code&gt;, &lt;code&gt;ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE&lt;/code&gt;, and certain configuration changes, such as &lt;code&gt;max degree of parallelism&lt;/code&gt;, which clear the cache for the entire instance. Invalidation, by contrast, triggers recompilation within the same entry, with the same &lt;code&gt;plan_handle&lt;/code&gt;.&lt;/p&gt;&#10;&lt;/div&gt;</description></item></channel></rss>