How to Find the Reason Why TempDB is Full in Your SQL Server

If the temp DB database of your SQL Server runs out of space, it can cause major disruptions in your production environment and can interrupt user applications from successful completion. If you are using a script to track temp DB size, append the script from this article to identify the root cause for the temp DB filling.

Tempdb full – a common scenario

Tempdb Poorly written queries might create several temporary objects resulting in a growing tempdb database. This will end in disk space alerts and might cause server problems. When many SQL Server database administrators’ find it very difficult to shrink the tempdb, they immediately opt for server restart. If had tried all methods to shrink the tempdb database and if it is still not shrinking, the last option is to restart SQL Service through configuration manager. Thus, your disk space alerts would stop and server problems also would stop. However, restarting tempdb might not be available to you if the issue had occurred on a production server.

DBCC

DBCCIn such cases, there are several DBCC commands which when run would allow you to shrink tempdb. If you had set up scripts to proactively monitor the tempdb size, you can use this script to find out fillers of tempdb. As of now, this script would get executed on all linkedservers. You can control it easily by putting a where clause. You can make the script to be executed only when there is a problem with your tempdb. The table @Tserver is used to store all your linked server names. For a tempdb database, SQL corruption would change the status of the database as SUSPECT and this will interrupt SQL Server service from starting.

DECLARE @Tserver TABLE 
  ( 
     cserver VARCHAR(200) 
  ) 

INSERT INTO @Tserver 
VALUES      ('SERVERNAME') 

DECLARE @LogTable TABLE 
  ( 
     cservername VARCHAR(200), 
     cssionid    SMALLINT, 
     callocmb    BIGINT, 
     cdeallocmb  BIGINT, 
     ctext       VARCHAR(4000), 
     cstatement  VARCHAR(4000) 
  ) 
DECLARE c1 CURSOR FOR 
  SELECT * 
  FROM   @Tserver 
DECLARE @cmd    NVARCHAR(4000), 
        @server VARCHAR(200) 

OPEN c1 

FETCH next FROM c1 INTO @server 

WHILE @@FETCH_STATUS = 0 
  BEGIN 
      SET @cmd = 'EXEC(''use tempdb Declare @Table1 table  ( cdeallopages bigint, callopages bigint, cssionid smallint, creqstid int ) insert into @Table1 SELECT SUM(internal_objects_dealloc_page_count), SUM(internal_objects_alloc_page_count), session_id, request_id  FROM sys.dm_db_task_space_usage WITH (NOLOCK) WHERE session_id <> @@SPID GROUP BY session_id, request_id declare @Table2 table ( cssionid smallint, callocmb bigint, cdeallocmb bigint, ctext varchar(4000), cstatement varchar(4000) ) insert into @Table2 SELECT TBL1.cssionid, TBL1.callopages * 1.0 / 128 , TBL1.cdeallopages * 1.0 / 128 , TBL3.text, ISNULL( NULLIF( SUBSTRING( TBL3.text,  TBL2.statement_start_offset / 2,  CASE WHEN TBL2.statement_end_offset < TBL2.statement_start_offset  THEN 0  ELSE( TBL2.statement_end_offset - TBL2.statement_start_offset ) / 2 END ), '''''''' ), TBL3.text ) FROM @Table1 AS TBL1 INNER JOIN sys.dm_exec_requests TBL2 WITH (NOLOCK) ON  TBL1.cssionid = TBL2.session_id AND TBL1.creqstid = TBL2.request_id OUTER APPLY sys.dm_exec_sql_text(TBL2.sql_handle) AS TBL3 OUTER APPLY sys.dm_exec_query_plan(TBL2.plan_handle) AS TBL4 WHERE TBL3.text IS NOT NULL OR TBL4.query_plan IS NOT NULL ORDER BY 3 DESC; Select * from @Table2'') at [' + @server + ']' 

      PRINT @cmd 

      INSERT INTO @LogTable 
                  (cssionid, 
                   callocmb, 
                   cdeallocmb, 
                   ctext, 
                   cstatement) 
      EXEC(@cmd) 

      UPDATE @LogTable 
      SET    cservername = @server 
      WHERE  cservername IS NULL 

      FETCH next FROM c1 INTO @server 
  END 

CLOSE c1 

DEALLOCATE c1 

SELECT * 
FROM   @LogTable

Author Introduction:

Neil Varley is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover Outlook and excel recovery software products. For more information visit www.datanumen.com

Comments are closed.