{"id":72,"date":"2023-02-09T20:01:21","date_gmt":"2023-02-09T18:01:21","guid":{"rendered":"http:\/\/130.61.57.200\/?p=72"},"modified":"2023-07-30T02:22:39","modified_gmt":"2023-07-30T00:22:39","slug":"all-the-log-info-you-need","status":"publish","type":"post","link":"http:\/\/130.61.57.200\/index.php\/2023\/02\/09\/all-the-log-info-you-need\/","title":{"rendered":"All the log info you need in MS SQL Server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The batch below will reveal all the information you want for the log files of the databases in current instance. Recovery model, location, size, used log, disk available on log file disk, last log backup, log size since last log backup, log reuse wait, VLFs etc&#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>declare @dbname nvarchar(100),@sql nvarchar (1000);\ndeclare dbs cursor for\nselect name from sys.databases where state_desc=&#039;ONLINE&#039;;\nIF OBJECT_ID(&#039;tempdb..#logUse&#039;) IS NOT NULL\n    DROP TABLE #logUse\ncreate table   #logUse (&#091;Database Name] varchar(100),&#091;Log File] VARCHAR(200),&#091;Recovery Model] varchar(10), &#091;Log Reuse Wait] VARCHAR(50),&#091;Disk Total GB] INT, &#091;Disk Available GB] INT, &#091;Log Total MB] int,&#091;Log Used MB] int,&#091;Log Used %] smallint,\n&#091;Log MB since las backup] int,&#091;Last Log Backup Start] datetime,&#091;Last Log BackupEnd] datetime, &#091;Last Log Backup MB] int);\nOPEN dbs;\nFETCH NEXT FROM dbs into @dbname;\n\nWHILE @@FETCH_STATUS=0\nBEGIN\nset @sql=N&#039;insert into #logUse\nselect top 1 db_name(SP.database_id),F.physical_name,D.recovery_model_desc,D.log_reuse_wait_desc,OS.total_bytes\/1024\/1024\/1024,OS.available_bytes\/1024\/1024\/1024,total_log_size_in_bytes\/1024\/1024 ,\nused_log_space_in_bytes\/1024\/1024 ,ROUND(used_log_space_in_percent,2)  ,\nlog_space_in_bytes_since_last_backup\/1024\/1024,backup_start_date,backup_finish_date,round(backup_size\/1024\/1024,0)  from &#091;&#039;+@dbname+&#039;].sys.dm_db_log_space_usage sp JOIN SYS.DATABASES D ON D.database_id=SP.database_id\nJOIN sys.master_files f ON  F.database_id=SP.database_id AND F.type_desc=&#039;&#039;LOG&#039;&#039; left join msdb.dbo.backupset bs on bs.database_name=d.name and bs.type=&#039;&#039;L&#039;&#039; CROSS   APPLY sys.dm_os_volume_stats(f.database_id, f.file_id) OS\norder by bs.backup_finish_date desc&#039;\n\n--print @sql\nexec sp_executesql @sql\nFETCH NEXT FROM dbs into @dbname;  \nEND;\nclose dbs;\ndeallocate dbs;\n\nselect l.*, vlf.&#091;VLF Count],vlf.&#091;Active VLF],vlf.&#091;Active VLF Size (MB)],vlf.&#091;In-active VLF],vlf.&#091;In-active VLF Size (MB)],case  when vlf.&#091;VLF Count]&gt;100 then &#039;Perhaps you should Shrink and recreate Log File&#039; else &#039;VLF count is not dangerously high&#039; end as &#039;VLF Status&#039;\nfrom #loguse l join (SELECT &#091;name], \nCOUNT(l.database_id) AS &#039;VLF Count&#039;,\nSUM(CAST(vlf_active AS INT)) AS &#039;Active VLF&#039;,\nSUM(vlf_active*vlf_size_mb) AS &#039;Active VLF Size (MB)&#039;,\nCOUNT(l.database_id)-SUM(CAST(vlf_active AS INT)) AS &#039;In-active VLF&#039;,\nSUM(vlf_size_mb)-SUM(vlf_active*vlf_size_mb) AS &#039;In-active VLF Size (MB)&#039;\nFROM sys.databases s\nCROSS APPLY sys.dm_db_log_info(s.database_id) l\nGROUP BY &#091;name], s.database_id\n) vlf on vlf.name=l.&#091;Database Name]\ndrop table #loguse\n<\/code><\/pre>\n\n\n<div class=\"taxonomy-category wp-block-post-terms\"><a href=\"http:\/\/130.61.57.200\/index.php\/category\/mssql\/\" rel=\"tag\">MSSQL<\/a><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>The batch below will reveal all the information you want for the log files of the databases in current instance. Recovery model, location, size, used log, disk available on log file disk, last log backup, log size since last log backup, log reuse wait, VLFs etc&#8230;<\/p>\n","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[30,32,31],"class_list":["post-72","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mssql","tag-log","tag-log-wait-reuse","tag-recovery-model"],"blocksy_meta":[],"_links":{"self":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":3,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":102,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/72\/revisions\/102"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}