Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Why does session not release space in TEMPDB after dropping temporary tables?

$
0
0

I'm using sql 2008 r2.

I've noticed when a session creates and populates a temporary table and then drops it, the space in tempdb does NOT get released. I've run into a situation where the same session creates and drops many fairly large temp tables, and eventually I run out of space in the tempdb.

Is there a way to make it release the space in the tempdb without disconnecting the session?

This is fairly easy to demonstrate. In the below code, even though I keep dropping the table, the tempdb space used by the session keeps increasing as reported by sys.dm_db_session_space_usage.

-- create and populate temp table, then drop it
CREATE TABLE #test (x CHAR(8000)) GO INSERT INTO #test VALUES ('x') GO 100 DROP TABLE #test GO

-- How much space are we using now?

SELECT user_objects_alloc_page_count FROM sys.dm_db_session_space_usage WHERE session_id = @@spid GO
-- Create and populate a temp table again
CREATE TABLE #test (x CHAR(8000)) GO INSERT INTO #test VALUES ('x') GO 100 DROP TABLE #test GO
-- Now we are using even MORE space SELECT user_objects_alloc_page_count FROM sys.dm_db_session_space_usage WHERE session_id = @@spid GO
DROP TABLE #test
GO




Chuck


Viewing all articles
Browse latest Browse all 12963

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>