{"id":3033,"date":"2012-11-22T02:32:29","date_gmt":"2012-11-22T02:32:29","guid":{"rendered":"http:\/\/craftydba.com\/?p=3033"},"modified":"2017-10-11T19:27:54","modified_gmt":"2017-10-11T19:27:54","slug":"shrinking-the-transaction-log","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=3033","title":{"rendered":"Shrinking the Transaction Log"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-shrink-file.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-shrink-file-150x150.jpg\" alt=\"\" title=\"dbcc-shrink-file\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-3075\" \/><\/a><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190925.aspx\">The transaction log<\/a> file records all transactions and the database modifications made by each transaction.  Heavy application processing against the database can make the transaction log grow quickly.  Therefore, the transaction log must be truncated on a ADHOC basis to keep it from filling up.  <\/p>\n<p>Each physical log file is divided into smaller logical units called virtual log files (VLFs).  Truncation provides free space by deleting inactive virtual log files.  To avoid running out of log file space or physical disk space if growth is set to unlimited, one should understand when a truncation occurs.  <\/p>\n<p>If the recovery model is set to simple, a truncation happens after a checkpoint.   Under the full or bulk-logged recovery model, a truncation occurs if a log backup is proceeded by a backup and a checkpoint.  Automatic <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189573.aspx#AutomaticChkpt\">database checkpoints<\/a> occur when the server&#8217;s target recovery time is zero, the default.  This process happens every 1 minute.<\/p>\n<p>I will be using continuing with the MATH database that contains all prime number between 2 and 6 million.  The log file has grown from 32 MB to 458 MB since no regular maintenance has been performed.<\/p>\n<p>We can try reducing the transaction log file by using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189493(v=sql.110).aspx\">DBCC SHRINKFILE<\/a> command.  However, this does not work.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"database console commands - dbcc shrinkfile()\">\r\n-- Try to shrink the log\r\nDBCC SHRINKFILE(MATH_ALL_LOG, 32);\r\nGO\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/unable-to-shrink-file.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/unable-to-shrink-file.jpg\" alt=\"\" title=\"unable-to-shrink-file\" width=\"1136\" height=\"164\" class=\"alignleft size-full wp-image-3047\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/unable-to-shrink-file.jpg 1136w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/unable-to-shrink-file-300x43.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/unable-to-shrink-file-1024x147.jpg 1024w\" sizes=\"auto, (max-width: 1136px) 100vw, 1136px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>I wonder if there are any open transactions that are causing our shrink command to fail?  Using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms182792.aspx\">DBCC OPENTRAN<\/a> command, we can see there are no open transactions.  <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"database console commands - dbcc opentran()\">\r\n-- Make sure there are not open transactions\r\nDBCC OPENTRAN;\r\nGO\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-open-trans-reports-nothing.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-open-trans-reports-nothing.jpg\" alt=\"\" title=\"dbcc-open-trans-reports-nothing\" width=\"1137\" height=\"218\" class=\"alignleft size-full wp-image-3054\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-open-trans-reports-nothing.jpg 1137w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-open-trans-reports-nothing-300x57.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-open-trans-reports-nothing-1024x196.jpg 1024w\" sizes=\"auto, (max-width: 1137px) 100vw, 1137px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>We want to look at the actual composition of the transaction log file (VLFs), by using the undocumented DBCC LOGINFO command.  <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"database console commands - dbcc loginfo()\">\r\n-- Size of virtual log files\r\nDBCC LOGINFO;\r\nGO\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t1.jpg\" alt=\"\" title=\"dbcc-loginfo-t1\" width=\"1138\" height=\"481\" class=\"alignleft size-full wp-image-3056\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t1.jpg 1138w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t1-300x126.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t1-1024x432.jpg 1024w\" sizes=\"auto, (max-width: 1138px) 100vw, 1138px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>At this point, we find the root cause of our problem.  The log file pointer is at the end of the file.  Because the point is the active point of the log, the DBCC SHRINKFILE command will not work.<\/p>\n<p>To move the log file point to be beginning of the file, we can create a temporary table and make a bunch of modifications to the first record.  Since all these modifications will be recorded in the transaction log file, the pointer will move to the front of the <a href=\"http:\/\/www.sqlskills.com\/BLOGS\/PAUL\/post\/Inside-the-Storage-Engine-More-on-the-circular-nature-of-the-log.aspx\">circular natured log<\/a>.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"tsql code - move log pointer\">\r\n-- Create temp table and move pointer\r\nDECLARE @VAR_CNT INT;\r\nSELECT @VAR_CNT = 1;\r\n\r\nCREATE TABLE [dbo].[TMP_LOGPTR] (MYVAL INT);\r\nINSERT INTO [dbo].[TMP_LOGPTR] VALUES (@VAR_CNT);\r\n\r\nWHILE (@VAR_CNT  &lt; 5000)\r\nBEGIN\r\n    UPDATE [dbo].[TMP_LOGPTR] SET MYVAL = MYVAL + 1;\r\n    SET @VAR_CNT = @VAR_CNT + 1\r\nEND\r\n\r\nDROP TABLE [dbo].[TMP_LOGPTR]\r\nGO\r\n<\/pre>\n<\/p>\n<p>Taking another look at the transaction log file details, we see that the log file pointer is at the front of the file but the end of the file has one VLF in use.  To have a truncation to be performed, we need to execute the simple <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186865.aspx\">BACKUP LOG<\/a> command.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"backup log command\">\r\n-- Backup the log file\r\nBACKUP LOG [MATH] TO DISK = 'C:\\MSSQL\\BACKUP\\MATH.BAK';\r\n<\/span><\/pre>\n<p><\/P><\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/backup-log-to-disk.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/backup-log-to-disk.jpg\" alt=\"\" title=\"backup-log-to-disk\" width=\"1137\" height=\"177\" class=\"alignleft size-full wp-image-3059\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/backup-log-to-disk.jpg 1137w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/backup-log-to-disk-300x46.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/backup-log-to-disk-1024x159.jpg 1024w\" sizes=\"auto, (max-width: 1137px) 100vw, 1137px\" \/><\/a><\/p>\n<p>&nbsp;<\/P><\/p>\n<p>Looking at the transaction log file details, we can see that all VLF&#8217;s except for the active one are clear.<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t3.jpg\" alt=\"\" title=\"dbcc-loginfo-t3\" width=\"1141\" height=\"485\" class=\"alignleft size-full wp-image-3058\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t3.jpg 1141w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t3-300x127.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/dbcc-loginfo-t3-1024x435.jpg 1024w\" sizes=\"auto, (max-width: 1141px) 100vw, 1141px\" \/><\/a><\/p>\n<p>We now can execute the DBCC SHRINKFILE and get a successful reduction in size.<\/p>\n<p>Last but not least, we will use the following T-SQL snippet to review the transaction log usage, transaction log size and VLF allocation.  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189768.aspx\">DBCC SQLPERF<\/a> command reports log file space usage.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"database console commands - dbcc sqlperf()\">\r\n-- Show all database\r\nDBCC SQLPERF(LOGSPACE);\r\nGO\r\n\r\n-- Size of virtual log files\r\nDBCC LOGINFO;\r\nGO\r\n\r\n-- Log file size in pages\r\nSELECT * FROM sys.database_files WHERE file_id = 2\r\nGO\r\n<\/pre>\n<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/final-log-file-size.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/final-log-file-size.jpg\" alt=\"\" title=\"final-log-file-size\" width=\"1027\" height=\"572\" class=\"alignleft size-full wp-image-3066\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/final-log-file-size.jpg 1027w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/final-log-file-size-300x167.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2012\/11\/final-log-file-size-1024x570.jpg 1024w\" sizes=\"auto, (max-width: 1027px) 100vw, 1027px\" \/><\/a><\/p>\n<p>In summary, there are many factors that can delay log truncation.  You should look at the following books online <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms190925.aspx\">article <\/a>for more details.  The above process will alleviate the issue of an active pointer at the end of a transaction log file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The transaction log file records all transactions and the database modifications made by each transaction. Heavy application processing against the database can make the transaction log grow quickly. Therefore, the transaction log must be truncated on a ADHOC basis to keep it from filling up. Each physical log file is divided into smaller logical units called virtual log files (VLFs). Truncation provides free space by deleting inactive virtual log files. To avoid running out of log file space or physical disk space if growth is set to unlimited, one should&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[254,311,602,623,624,625,12,15,626,29],"class_list":["post-3033","post","type-post","status-publish","format-standard","hentry","category-db-admin","tag-backup-log","tag-checkpoint","tag-dba","tag-dbcc-loginfo","tag-dbcc-shrinkfile","tag-dbcc-sqlperf","tag-free-code","tag-john-f-miner-iii","tag-transaction-log-shrinking","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/3033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3033"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/3033\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}