I'm looking to create a monitoring tool to track changes in transaction log usage on a production server we maintain.
Previously, I used DBCC SQLPERF(LOGSPACE);
, which provided a list of all databases and their current transaction log memory status. However, Microsoft seem to suggest from 2012, the log details should be viewed from sys.dm_db_log_space_usage
, which provides similar detail, but seems to be database specific, rather than giving a view of the server overall (i.e. you must be connected to the database you wish to use).
I guess my question is two fold:
sys.dm_db_log_space_usage
for all databases?DBCC SQLPERF(LOGSPACE);
should not be used?Is there any reason the DBCC SQLPERF(LOGSPACE); should not be used?
Starting with SQL Server 2012 (11.x), use the sys.dm_db_log_space_usage DMV instead of DBCC SQLPERF(LOGSPACE), to return space usage information for the transaction log per database
Is there a way to use sys.dm_db_log_space_usage for all databases?
You can use the following cursor. Maybe it required a more development;
CREATE TABLE [dbo].[Tbl_DbSizes](
[database_id] [int] NULL,
[total_log_size_in_bytes] [bigint] NULL,
[used_log_space_in_bytes] [bigint] NULL,
[used_log_space_in_percent] [real] NULL,
[log_space_in_bytes_since_last_backup] [bigint] NULL
) ON [PRIMARY]
GO
DECLARE
@queryAsList VARCHAR(MAX) ,@DbName AS VARCHAR(100)
DECLARE Db_List CURSOR
FOR
SELECT name FROM sys.databases
OPEN Db_List;
FETCH NEXT FROM Db_List INTO
@DbName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @queryAsList = 'INSERT INTO Tbl_DbSizes select * from '+ @DbName + '.' + 'sys.dm_db_log_space_usage'
EXEC(@queryAsList)
FETCH NEXT FROM Db_List INTO
@DbName
END;
CLOSE Db_List;
DEALLOCATE Db_List;
SELECT name as databasename , total_log_size_in_bytes ,
used_log_space_in_bytes,used_log_space_in_percent,log_space_in_bytes_since_last_backup
FROM Tbl_DbSizes INNER JOIN sys.databases databaseinfo
ON databaseinfo.database_id= Tbl_DbSizes.database_id
TRUNCATE TABLE [Tbl_DbSizes]
+--------------------+-------------------------+-------------------------+---------------------------+--------------------------------------+
| databasename | total_log_size_in_bytes | used_log_space_in_bytes | used_log_space_in_percent | log_space_in_bytes_since_last_backup |
+--------------------+-------------------------+-------------------------+---------------------------+--------------------------------------+
| master | 2088960 | 729088 | 34.90196 | 270336 |
| tempdb | 8380416 | 675840 | 8.064516 | 299008 |
| model | 8380416 | 1617920 | 19.30596 | 73728 |
| msdb | 9428992 | 1208320 | 12.81494 | 86016 |
| DWDiagnostics | 75489280 | 6467584 | 8.567553 | 253952 |
| DWConfiguration | 8380416 | 626688 | 7.478006 | 253952 |
| DWQueue | 8380416 | 1404928 | 16.76442 | 253952 |
| DemoDb | 8380416 | 1732608 | 20.67449 | 266240 |
| ReportServer | 75489280 | 13873152 | 18.37765 | 274432 |
| ReportServerTempDB | 75489280 | 1925120 | 2.55019 | 245760 |
+--------------------+-------------------------+-------------------------+---------------------------+--------------------------------------+