One of the aspects to monitor on a SQL Server instance is the size of the logs available for each database, respectively the degree to which the logs are used. Starting with SQL Server 2005 this could be achieved by using the 'Log File(s) Used Size (KB)' and 'Log File(s) Size (KB)' counters via the sys.dm_os_performance_counters DMV as follows:
-- log files - size (kb) SELECT lfu.instance_name database_name , lfu.cntr_value size_kb , Cast(lfu.cntr_value/1024.00 as decimal (18,2)) size_MB FROM sys.dm_os_performance_counters lfu WHERE lfu.counter_name LIKE 'Log File(s) Size (KB)%' AND lfu.object_name LIKE 'SQLServer:Databases%' AND lfu.instance_name IN ('tempdb', 'master', 'model', 'msdb') ORDER BY lfu.instance_name -- log files - used size (kb) SELECT lfs.instance_name database_name , lfs.cntr_value used_size_kb , Cast(lfs.cntr_value/1024.00 as decimal (18,2)) used_size_MB FROM sys.dm_os_performance_counters lfs WHERE lfs.counter_name LIKE 'Log File(s) Used Size (KB)%' AND lfs.object_name LIKE 'SQLServer:Databases%' AND lfs.instance_name IN ('tempdb', 'master', 'model', 'msdb') ORDER BY lfs.instance_name
The two queries can be combined into one as follows:
-- database log space allocation (SQL Server 2005+) SELECT db.name database_name , db.log_reuse_wait_desc , Cast(lfs.cntr_value/1024.00 as decimal(28,2)) size_MB , Cast(lfu.cntr_value/1024.00 as decimal(28,2)) AS used_MB , Cast(100.00*lfu.cntr_value/lfs.cntr_value as decimal(10,2)) used_percent , CASE WHEN CAST(lfu.cntr_value AS float) / CAST(lfs.cntr_value AS float) > .5 THEN CASE WHEN db.name = 'tempdb' AND log_reuse_wait_desc NOT IN ('CHECKPOINT', 'NOTHING') THEN 'WARNING' WHEN db.name <> 'tempdb' THEN 'WARNING' ELSE 'OK' END ELSE 'OK' END log_status FROM sys.databases db JOIN sys.dm_os_performance_counters lfs ON db.name = lfs.instance_name AND lfs.counter_name LIKE 'Log File(s) Size (KB)%' JOIN sys.dm_os_performance_counters lfu ON db.name = lfu.instance_name AND lfu.counter_name LIKE 'Log File(s) Used Size (KB)%' WHERE db.name IN ('tempdb', 'master', 'model', 'msdb') ORDER BY db.name
database_name | log_reuse_wait_desc | size_MB | used_MB | used_percent | log_status |
master | NOTHING | 1.99 | 1.11 | 55.78 | WARNING |
model | NOTHING | 7.99 | 1.74 | 21.80 | OK |
msdb | NOTHING | 28.80 | 1.96 | 6.81 | OK |
tempdb | NOTHING | 999.99 | 0.69 | 0.07 | OK |
Starting with SQL Server 2012 the same information can be obtained via the sys.dm_db_log_space_usage DMV, however the view returns information only for the current database:
-- getting the log space only for a database (SQL Server 2012+) SELECT db_name(database_id) database_name , Cast(total_log_size_in_bytes/1024.00/1024.00 as decimal(28,2)) size_MB , Cast(used_log_space_in_bytes/1024.00/1024.00 as decimal(28,2)) used_MB , Cast(used_log_space_in_percent as decimal(28,2)) used_percent FROM sys.dm_db_log_space_usage
database_name | size_MB | used_MB | used_percent |
master | 1.99 | 1.19 | 59.61 |
With less flexibility one can obtain the size in MB and the used percentage by using the DBCC utility as follows:
-- retrieving the log usage for all databases DBCC SQLPERF(LOGSPACE);
Database Name | Log Size (MB) | Log Space Used (%) | Status |
master | 1,992188 | 33,13726 | 0 |
tempdb | 999,9922 | 0,06352589 | 0 |
model | 7,992188 | 21,11437 | 0 |
msdb | 28,80469 | 6,617846 | 0 |
AdventureWorks2014 | 33,99219 | 14,76672 | 0 |
AdventureWorksDW2014 | 17,99219 | 22,6878 | 0 |
Notes:
1. All the mentioned objects require VIEW SERVER STATE permissions.
2. The solution based on the performance counters returns slightly different values than the other solutions, though the differences are neglectable.
Resources:
[1] SQL Docs (2017) sys.dm_os_performance_counters [source]
[2] SQL Docs (2017) DBCC SQLPERF [source]
[3] SQL Docs (2017) sys.dm_db_log_space_usage [source]
No comments:
Post a Comment