SQL Swiss Army Knife #9 - Fixing VLFs

Published Mar 23 2019 12:12 PM 363 Views
Microsoft
First published on MSDN on May 24, 2011
Download script here: Fix_VLFs.sql



Hello all,
Here is another post on SQL scripts that may help DBAs, following the series "SQL Swiss Army Knife", this time revisiting the topic of VLFs. I blogged on this subject several times before and if you want to read more about it just click here .

Anyhow, a few months back I knew of a case where a database had over 1.2 million VLFs, and it took a very long time to recover when a restart was performed on the instance. More recently I as made aware of a database with over 930k VLFs. Thankfully, the database owner wanted to preemptively deal with the situation. The database owner was aware of the impact of a high VLF number and wanted a way of quickly finding and dealing with this kind of issue on other servers. This is why I wrote a script that gets an overview of the current VLF status in all databases of a given server, and if the number of VLFs are above a pre-determined threshold, also makes a suggestion of how many and how large the VLFs should be for that particular database.

The output will show:

  • The database name;

  • The transaction log current size and the size it will be after applying suggested changes. Both in MB;

  • The current number of VLFs and the number of VLFs that will remain after applying suggested changes;

  • The amount of growth iterations necessary to get to the suggested size;

  • The transaction log initial size and the autogrow size that should be set;


It will resemble this:



Note that database and file names are purposely blacked out to preserve sensitive data.

In addition, a script is generated with the typical steps needed to deal with the issue, depending on whether the database is in Simple recovery model or not.

Something like this example:


Hope you find it useful as much as I did.

Until next time!



EDIT (09-08-2011): missing variable set for sql version. Thanks go to Calvin for finding this bug.

EDIT (26-03-2012): Updated script for SQL 2012 support.

EDIT (19-09-2012): Simplified logic.

EDIT (20-09-2012): Changed grow settings if not SQL Server 2012.

EDIT (11/03/2016): Moved to Github.

Disclaimer: I hope that the information on these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Ezequiel. Further, Ezequiel shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
%3CLINGO-SUB%20id%3D%22lingo-sub-383879%22%20slang%3D%22en-US%22%3ESQL%20Swiss%20Army%20Knife%20%239%20-%20Fixing%20VLFs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383879%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20May%2024%2C%202011%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Download%20script%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2Ftigertoolbox%2Ftree%2Fmaster%2FFixing-VLFs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20Fix_VLFs.sql%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Hello%20all%2C%20%3CBR%20%2F%3E%20Here%20is%20another%20post%20on%20SQL%20scripts%20that%20may%20help%20DBAs%2C%20following%20the%20series%20%22SQL%20Swiss%20Army%20Knife%22%2C%20this%20time%20revisiting%20the%20topic%20of%20VLFs.%20I%20blogged%20on%20this%20subject%20several%20times%20before%20and%20if%20you%20want%20to%20read%20more%20about%20it%20just%20click%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fblogdoezequiel%2Farchive%2Ftags%2Fvlf%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Anyhow%2C%20a%20few%20months%20back%20I%20knew%20of%20a%20case%20where%20a%20database%20had%20over%201.2%20million%20VLFs%2C%20and%20it%20took%20a%20very%20long%20time%20to%20recover%20when%20a%20restart%20was%20performed%20on%20the%20instance.%20More%20recently%20I%20as%20made%20aware%20of%20a%20database%20with%20over%20930k%20VLFs.%20Thankfully%2C%20the%20database%20owner%20wanted%20to%20preemptively%20deal%20with%20the%20situation.%20The%20database%20owner%20was%20aware%20of%20the%20impact%20of%20a%20high%20VLF%20number%20and%20wanted%20a%20way%20of%20quickly%20finding%20and%20dealing%20with%20this%20kind%20of%20issue%20on%20other%20servers.%20This%20is%20why%20I%20wrote%20a%20script%20that%20gets%20an%20overview%20of%20the%20current%20VLF%20status%20in%20all%20databases%20of%20a%20given%20server%2C%20and%20if%20the%20number%20of%20VLFs%20are%20above%20a%20pre-determined%20threshold%2C%20also%20makes%20a%20suggestion%20of%20how%20many%20and%20how%20large%20the%20VLFs%20should%20be%20for%20that%20particular%20database.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20output%20will%20show%3A%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EThe%20database%20name%3B%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EThe%20transaction%20log%20current%20size%20and%20the%20size%20it%20will%20be%20after%20applying%20suggested%20changes.%20Both%20in%20MB%3B%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EThe%20current%20number%20of%20VLFs%20and%20the%20number%20of%20VLFs%20that%20will%20remain%20after%20applying%20suggested%20changes%3B%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EThe%20amount%20of%20growth%20iterations%20necessary%20to%20get%20to%20the%20suggested%20size%3B%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EThe%20transaction%20log%20initial%20size%20and%20the%20autogrow%20size%20that%20should%20be%20set%3B%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20It%26nbsp%3Bwill%20resemble%20this%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97467iDC19C1A50F5EED74%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Note%20that%20database%20and%20file%20names%20are%20purposely%20blacked%20out%20to%20preserve%20sensitive%20data.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20addition%2C%20a%20script%20is%20generated%20with%20the%20typical%20steps%20needed%20to%20deal%20with%20the%20issue%2C%20depending%20on%20whether%20the%20database%20is%20in%20Simple%20recovery%20model%20or%20not.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Something%20like%20this%20example%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97468i91A37D423A1F6FC3%22%20%2F%3E%20%3CBR%20%2F%3E%3CDIV%3EHope%20you%20find%20it%20useful%20as%20much%20as%20I%20did.%3C%2FDIV%3E%3CBR%20%2F%3E%20Until%20next%20time!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20EDIT%20(09-08-2011)%3A%20missing%20variable%20set%20for%20sql%20version.%20Thanks%20go%20to%26nbsp%3BCalvin%20for%20finding%20this%20bug.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20EDIT%20(26-03-2012)%3A%20Updated%20script%20for%20SQL%202012%20support.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20EDIT%20(19-09-2012)%3A%20Simplified%20logic.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20EDIT%20(20-09-2012)%3A%20Changed%20grow%20settings%20if%20not%20SQL%20Server%202012.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20EDIT%20(11%2F03%2F2016)%3A%20Moved%20to%20Github.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Disclaimer%3A%20I%20hope%20that%20the%20information%20on%20these%20pages%20is%20valuable%20to%20you.%20Your%20use%20of%20the%20information%20contained%20in%20these%20pages%2C%20however%2C%20is%20at%20your%20sole%20risk.%20All%20information%20on%20these%20pages%20is%20provided%20%22as%20-is%22%2C%20without%20any%20warranty%2C%20whether%20express%20or%20implied%2C%20of%20its%20accuracy%2C%20completeness%2C%20fitness%20for%20a%20particular%20purpose%2C%20title%20or%20non-infringement%2C%20and%20none%20of%20the%20third-party%20products%20or%20information%20mentioned%20in%20the%20work%20are%20authored%2C%20recommended%2C%20supported%20or%20guaranteed%20by%20Ezequiel.%20Further%2C%20Ezequiel%20shall%20not%20be%20liable%20for%20any%20damages%20you%20may%20sustain%20by%20using%20this%20information%2C%20whether%20direct%2C%20indirect%2C%20special%2C%20incidental%20or%20consequential%2C%20even%20if%20it%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383879%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20May%2024%2C%202011%20Download%20script%20here%3A%20Fix_VLFs.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383879%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 12:12 PM
Updated by: