SQL Server allows stored procedures to reference temporary tables that were created in an outer scope. However, doing so can cause the plan cache pollution problem described here.
The Polluter
The problem can be observed with a simple repro script. The child stored procedure performs a series of updates on a temp table. The parent procedure creates the temp table, calls the child stored procedure, and returns data from the temp table.
CREATE OR ALTER PROCEDURE dbo.update_temp_table
AS
BEGIN
SET NOCOUNT ON;
-- the #outer_scope temp table is created in an outer scope
UPDATE #outer_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
UPDATE #outer_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t3) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
UPDATE #outer_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t4) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
UPDATE #outer_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t5) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
END;
GO
CREATE OR ALTER PROCEDURE dbo.parent_proc
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #outer_scope (ID INT NOT NULL, CNT INT NULL);
INSERT INTO #outer_scope (ID, CNT)
VALUES (1, NULL);
EXEC dbo.update_temp_table;
SELECT * FROM #outer_scope;
END;
GO
I executed the parent_proc procedure on three different sessions and was rewarded with three different cached plans:
The Cleaner
One way to resolve this issue is to change the child procedure to create a new local temp table, to copy the data from the original table into the new one using its own child procedure, to perform the updates against the new table, and to copy back the data into the original table, again in a child procedure. That was a lot of words so perhaps code will be easier to understand:
CREATE OR ALTER PROCEDURE dbo.update_temp_table_import_temp_data
WITH RECOMPILE
AS
BEGIN
IF OBJECT_ID(N'tempdb..#outer_scope', 'U') IS NOT NULL
BEGIN
INSERT INTO #local_scope
SELECT * FROM #outer_scope;
END;
END;
GO
CREATE OR ALTER PROCEDURE dbo.update_temp_table_export_temp_data
WITH RECOMPILE
AS
BEGIN
IF OBJECT_ID(N'tempdb..#outer_scope', 'U') IS NOT NULL
BEGIN
TRUNCATE TABLE #outer_scope;
INSERT INTO #outer_scope
SELECT * FROM #local_scope;
END;
END;
GO
CREATE OR ALTER PROCEDURE dbo.update_temp_table_NEW
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #local_scope (ID INT NOT NULL, CNT INT NULL);
EXEC dbo.update_temp_table_import_temp_data;
UPDATE #local_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
UPDATE #local_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t3) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
UPDATE #local_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t4) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
UPDATE #local_scope SET CNT = (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t5) + (SELECT COUNT(*) FROM master..spt_values t1 CROSS JOIN master..spt_values t2)
WHERE ID = 1;
EXEC update_temp_table_export_temp_data;
END;
GO
CREATE OR ALTER PROCEDURE dbo.parent_proc_NEW
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #outer_scope (ID INT NOT NULL, CNT INT NULL);
INSERT INTO #outer_scope (ID, CNT)
VALUES (1, NULL);
EXEC dbo.update_temp_table_NEW;
SELECT * FROM #outer_scope;
END;
GO
Running the new procedure in three different sessions no longer results in cache pollution:
This is because the references to outer-scoped temp tables have been moved to small, simple procedures that are built with a procedure level RECOMPILE hint. This approach has the following advantages:
- There is no cache pollution for any of the code.
- The update statements will be compiled significantly less often, especially on SQL Server 2019 and above.
It also has the following disadvantages:
- Data from the outer table needs to be written to the local table and data from the local table is written back to the outer table.
- The definition and columns of the temp table need to be duplicated in additional places in code.
I don’t think that this is the best general solution by any means, but it does fulfill a particular niche use case and I’ve been able to use this method in production code. For other ideas, Erland Sommarskog has a detailed write up of different ways of passing data between procedures.
Final Thoughts
When you need your plan cache to be cleaned there’s really only one person to call:
Thanks for reading!