Solved

SQl: Tempdb reserved space not being released

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

911 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

21 Experts available now in Live!

Get 1:1 Help Now