Solved

SQl: Tempdb reserved space not being released

Posted on 2014-01-19
5
1,366 Views
Last Modified: 2014-03-18
Hi All,

We have a stored proc that seem to execute but never deallocate the space it reserves in tempdb.  During the stored proc it creates 7 temp tables each with about 100,000 - 200,00 rows and end of the proc it drops those tables and does a truncate of another table. As this stored proc runs a number of times a day the space increases..and then our monitoring system starts making noise.

Just to make matters more interesting there is a network device between the application servers and the sql server. This device does mutliplexing so there is not a 1:1 relationship between the application server connection pool, and the number of connections on the sql server. So this rules out issuing a hard connection close at the application end, which they are doing anyway.

The tempdb is fairly large, and to date it has not grown. This store proc would need to run for about 8 days with out intervention before the temp started growing. We pre-grew the tempdb.

Any suggestion on this one? I understand that as a stop gap we could check the DMV's and kill off any process matched a predefined profile, but I see that more as a temp work around not as the proper solution.

Cheers
JL
0
Comment
Question by:jaLouden
  • 2
  • 2
5 Comments
 
LVL 13

Assisted Solution

by:magarity
magarity earned 150 total points
ID: 39793214
First, let's make sure the culprit really is the stored proc you suspect.  Here is a query from technet that will show what processes are using how much space in tempdb. Run it when the stored proc is running:
SELECT t1.session_id, t1.request_id, t1.task_alloc,
  t1.task_dealloc, t2.sql_handle, t2.statement_start_offset, 
  t2.statement_end_offset, t2.plan_handle
FROM (Select session_id, request_id,
    SUM(internal_objects_alloc_page_count) AS task_alloc,
    SUM (internal_objects_dealloc_page_count) AS task_dealloc 
  FROM sys.dm_db_task_space_usage 
  GROUP BY session_id, request_id) AS t1, 
  sys.dm_exec_requests AS t2
WHERE t1.session_id = t2.session_id
  AND (t1.request_id = t2.request_id)
ORDER BY t1.task_alloc DESC

Open in new window

0
 
LVL 2

Author Comment

by:jaLouden
ID: 39793238
Hey Guru,

I can confirm it is  the stored proc. Our monitoring solution is about to show what SPID is using what in tempdb down to the individual file level.

I did however run the command just to be 100% sure..and use the monitoring tool and query match up.

Cheers
JL
0
 
LVL 35

Accepted Solution

by:
David Todd earned 150 total points
ID: 39793366
Hi,

What I'd do/What I've done:
With a couple of big ETL procedures, I've searched from the end of the file and found the last reference to temp file abc. After that, I've put an explicit if exits drop.

I did get a performance improvement in doing so, and not just letting the procedure end and delete the temp objects automatically.

HTH
  David

PS Template code for dropping a temp table
if object_id( N'tempdb..<temptable_name, sysname, #t>', N'U' ) is not null 
	drop table <temptable_name, sysname, #t>;

Open in new window

0
 
LVL 2

Author Comment

by:jaLouden
ID: 39793369
Hey David,

Will give that a go, looks good. Stay tuned.

Cheers
JL
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39793378
ah

Some of my last comment should be

... explicit if exists drop.

Regards
  David
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question