Published on

January 18, 2017

Monitoring TempDB Space Usage in SQL Server

As a SQL Server database administrator, it is important to monitor the space usage of TempDB. TempDB is a system database in SQL Server that is used to store temporary objects, such as temporary tables and table variables, as well as intermediate query results.

In this article, we will discuss a script that can be used to monitor the space usage of TempDB and troubleshoot any issues related to its log file.

The Issue

Let’s consider a scenario where the database file (mdf) for TempDB is only 5 GB, but the transaction log file (ldf) has grown to a huge size of 80 GB. This indicates a potential problem with the space usage of TempDB.

The Solution

To troubleshoot this issue, we can use the following script to find out the space usage of TempDB:

SELECT SUM(unallocated_extent_page_count) AS [free_pages],
       (SUM(unallocated_extent_page_count) * 1.0 / 128) AS [free_space_MB],
       SUM(version_store_reserved_page_count) AS [version_pages_used],
       (SUM(version_store_reserved_page_count) * 1.0 / 128) AS [version_space_MB],
       SUM(internal_object_reserved_page_count) AS [internal_object_pages_used],
       (SUM(internal_object_reserved_page_count) * 1.0 / 128) AS [internal_object_space_MB],
       SUM(user_object_reserved_page_count) AS [user_object_pages_used],
       (SUM(user_object_reserved_page_count) * 1.0 / 128) AS [user_object_space_MB]
FROM sys.dm_db_file_space_usage;

This query will provide information about the free space, version store usage, and usage by internal and user objects in TempDB.

In our case, the query showed that most of the space in TempDB was allocated to the transaction log file, indicating that there might be active transactions still using the log file.

To identify the transactions using the log file, we can use the following script:

SELECT database_transaction_log_bytes_reserved, session_id
FROM sys.dm_tran_database_transactions AS tdt
INNER JOIN sys.dm_tran_session_transactions AS tst
ON tdt.transaction_id = tst.transaction_id
WHERE database_id = 2;

This script will provide information about the transactions and the amount of log space they have reserved in TempDB.

Finally, to identify the source of the queries causing the log file usage, we can use the following script:

SELECT *
FROM sys.sysprocesses;

This script will show the active processes and their associated queries. In our case, we found a query using the openquery function that was causing the excessive log file usage.

Conclusion

Monitoring the space usage of TempDB is crucial for maintaining the performance and stability of SQL Server. By using the scripts mentioned in this article, you can identify any issues related to TempDB space usage and take appropriate actions, such as killing the associated processes and shrinking the database files.

Do you have any similar scripts or solutions that you would like to share with other readers? Feel free to leave a comment below!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.