Solved

SQl: Tempdb reserved space not being released

Posted on 2014-01-19
5
1,327 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now