Temp Table Caching: Find the Procedures That Break It

Temp Table Caching: Find the Procedures That Break It

PAGELATCH waits pile up on tempdb allocation pages while a handful of stored procedures run constantly. The files have room. Nobody deployed anything dramatic. Yet the server keeps tripping over the same short-lived tables, and temp table caching, which should be softening the blow, may not be happening at all.

Why is temp table caching not working in my SQL Server stored procedures? Temp table caching is lost when a stored procedure alters the temp table after creating it, gives a constraint a name, builds an index after creation, or creates the table in dynamic SQL. Search your procedure text for those four patterns, starting with the procedures that run most often and use the most CPU.

The Temp Object Usage Profile report in Database Health Monitor names the procedures that create temp tables and table variables, how often they run, and what that habit costs. It works from the code side.

Temp Object Usage Profile is one of the reports in Database Health Monitor. It runs against your own servers, and it takes about a minute to have this same screen open on one of them.

Why watching tempdb alone leaves you short

The usual first move is to study tempdb itself. Which pages are latched? What is filling the files? Those are fair questions, but the answers describe where it hurts. They don't tell you which CREATE TABLE to change.

Reading procedure text by hand has its own trap. A plain search for CREATE TABLE # also hits commented-out code and error messages, and it says nothing about whether a procedure runs twice a day or hundreds of times a minute. The report blanks comments and string literals before scanning. A temp table created inside a string is still caught, and labeled as dynamic SQL.

What breaks temp table caching, and what to measure

Count creations over time, then ask whether each one can be reused. Temp tables created per minute is runs per minute multiplied by temp objects per call. Anything above 500 counts as a high creation rate. Runs per minute comes from executions divided by the minutes since the plan was cached.

The Cacheable column answers the second question with Yes, or No plus the reason:

  • A constraint on the temp table has an explicit name
  • ALTER TABLE changes it after creation
  • An index is created after the table, which also forces a recompile when it follows the load
  • The table is created in dynamic SQL

Table variables fail differently. The report flags one when its scan or seek in the cached plan is estimated at 1 row or fewer, while a statement in that procedure returns at least 100 rows per call. Below compatibility level 150, the advice points to deferred compilation from SQL Server 2019. At 150 or higher, it tells you the plan may predate that behavior.

Reading the grid

Open a database, then Real Time, then Temp Object Usage Profile. Rows are ranked by CPU share, meaning each procedure's slice of all procedure CPU in that database. A messy procedure that barely registers can wait.

Recompile rate shows plan generations after the first as a percentage of executions. Table variable estimate puts the smallest estimate beside the most rows returned per call. Finding says what to do, with the full set in the tooltip. Use the run filter (any, 1+, 10+ or 100+) to drop rarely executed procedures. Above the grid you also get an instance-wide temp table creation rate and cache hit ratio, sampled over three seconds.

Act on it, and know the edges

Fix the expensive rows first. For a non-cacheable table, remove the specific cause named in the column. For a flagged table variable, open the plan before editing anything: the returned row count covers the whole statement, so a large result from a different statement can raise a false flag. While you're in plans, Non Sargable Predicate: Why the Plan Never Warns You covers another problem that is easy to miss there.

Only procedures still in the plan cache appear, so a restart or memory pressure empties the list. Encrypted ones are counted but not read. A CREATE inside a loop counts once. Plans are examined for the busiest 40 procedures that declare a table variable. You need SQL Server 2012 or newer and VIEW SERVER STATE, and the page only reads. Column details are in the Temp Object Usage Profile documentation.

What to check on your own server

  • Search your busiest procedures for named constraints, ALTER TABLE on a temp table, later index builds, and temp tables created in dynamic SQL
  • Move attention first to any index built after a temp table is loaded, because it also recompiles the procedure
  • Multiply each suspect procedure's runs per minute by its temp tables per call, and flag anything over 500
  • Read the cached plan for table variable scans or seeks estimated at 1 row where a statement returns at least 100 rows per call
  • Note the database compatibility level before deciding whether deferred compilation for table variables can help, since it arrives at level 150

Try Database Health Monitor Today

Stop guessing which stored procedure is churning through temp tables, losing the cache, recompiling, or reading a table variable on a one-row estimate. Database Health Monitor shows it on every instance you connect, in the time it takes to open the report.

Download Database Health Monitor and run the Temp Object Usage Profile report against your own server. There is nothing to configure first, and you will know inside a few minutes whether it tells you something you did not already know.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To prove you are not a robot: *