Share

Fixing a database with a high VLF count



Fixing a database with a high VLF count is a simple process:

1. Check the current size of the transaction log.
2. Backup the transaction log.
3. Shrink the transaction log to as close to 0 KB as possible.
4. Check that the VLF count is less than 50 (best if less than 10 at this point).
     a. If the VLF count is greater than 50, repeat steps 1 through 3.
     b. You may also have to check that there are no active transactions, etc. The log_reuse_wait_desc column in sys.databases will help identify why the log file cannot be shrunk.
5. Grow the transaction log back out to the original size

When I log into a new server environment for a client, it is pretty common to see multiple databases that have high VLF counts. The steps above are easy enough to complete when you have one or two databases that need fixed, but it becomes a very tedious process when there are 20, 30, 40 databases that all need fixed.



The output of the scripts below will come out similar to:

[code]
 /****************************************
 * Fix High VLF for database: MattWorks
 * Starting VLF count: 219
 ****************************************/


 -- Step 1: Get current log file size

 use [MattWorks]
 SELECT name AS [File Name] , physical_name AS [Physical Name], size/128.0 AS [Total Size in MB],size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS [Available Space In MB], type_desc
 FROM sys.database_files;


-- Step 2: Take transaction log backup if needed, and then shrink the log file
 DBCC SHRINKFILE (N'MattWorks_log' , 0, TRUNCATEONLY);


-- Step 3: Check if current VLF count is less than 50.
 -- If not, take another log backup and repeat step #2 until VLF count is less than 50. check for active transactions, etc.
 DBCC LogInfo;


--Step 4: Grow log file back to original size
 USE [master];
 GO
 ALTER DATABASE [MattWorks] MODIFY FILE (NAME = N'MattWorks_log', SIZE = 1024MB);
 GO
 [/code]


Query to check size of Databases in SQL Server

Open SSMS ( SQL SERVER MANAGEMENT STUDIO ) ==> Right click on the instance ==> New Query and paste the below code to find the Auto Shrink status


Total db's in GB :

SELECT CONVERT(DECIMAL(10,2),(SUM(size * 8.00) / 1024.00 / 1024.00)) As UsedSpace
FROM master.sys.master_files





All DB's individually :

with fs
as
(
    select database_id, type, size * 8.0 / 1024 size
    from sys.master_files
)
select
    name,
    (select sum(size) from fs where type = 0 and fs.database_id = db.database_id) DataFileSizeMB,
    (select sum(size) from fs where type = 1 and fs.database_id = db.database_id) LogFileSizeMB
from sys.databases db

Query to Check All Jobs in SQL Server

Open SSMS ( SQL SERVER MANAGEMENT STUDIO ) ==> Right click on the instance ==> New Query and paste the below code to find the Auto Shrink status


SELECT
    [sJOB].[job_id] AS [JobID]
    , [sJOB].[name] AS [JobName]
    , [sDBP].[name] AS [JobOwner]
    , [sCAT].[name] AS [JobCategory]
    , [sJOB].[description] AS [JobDescription]
    , CASE [sJOB].[enabled]
        WHEN 1 THEN 'Yes'
        WHEN 0 THEN 'No'
      END AS [IsEnabled]
    , [sJOB].[date_created] AS [JobCreatedOn]
    , [sJOB].[date_modified] AS [JobLastModifiedOn]
    , [sSVR].[name] AS [OriginatingServerName]
    , [sJSTP].[step_id] AS [JobStartStepNo]
    , [sJSTP].[step_name] AS [JobStartStepName]
    , CASE
        WHEN [sSCH].[schedule_uid] IS NULL THEN 'No'
        ELSE 'Yes'
      END AS [IsScheduled]
    , [sSCH].[schedule_uid] AS [JobScheduleID]
    , [sSCH].[name] AS [JobScheduleName]
    , CASE [sJOB].[delete_level]
        WHEN 0 THEN 'Never'
        WHEN 1 THEN 'On Success'
        WHEN 2 THEN 'On Failure'
        WHEN 3 THEN 'On Completion'
      END AS [JobDeletionCriterion]
FROM
    [msdb].[dbo].[sysjobs] AS [sJOB]
    LEFT JOIN [msdb].[sys].[servers] AS [sSVR]
        ON [sJOB].[originating_server_id] = [sSVR].[server_id]
    LEFT JOIN [msdb].[dbo].[syscategories] AS [sCAT]
        ON [sJOB].[category_id] = [sCAT].[category_id]
    LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP]
        ON [sJOB].[job_id] = [sJSTP].[job_id]
        AND [sJOB].[start_step_id] = [sJSTP].[step_id]
    LEFT JOIN [msdb].[sys].[database_principals] AS [sDBP]
        ON [sJOB].[owner_sid] = [sDBP].[sid]
    LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH]
        ON [sJOB].[job_id] = [sJOBSCH].[job_id]
    LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH]
        ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]
ORDER BY [JobName]

Query to Attach database in SQL Server

Open SSMS ( SQL SERVER MANAGEMENT STUDIO ) ==> Right click on the instance ==> New Query and paste the below code to find the Auto Shrink status


USE [master]
GO
CREATE DATABASE [Database Name] ON
( FILENAME = 'path\database.mdf'),
( FILENAME = 'path\database.ldf')
FOR ATTACH
GO

Query to Find Buffer Cache Hit Ratio

Open SSMS ( SQL SERVER MANAGEMENT STUDIO ) ==> Right click on the instance ==> New Query and paste the below code to find the Auto Shrink status


SELECT (a.cntr_value * 1.0 / b.cntr_value) * 100.0 as BufferCacheHitRatio
FROM sys.dm_os_performance_counters  a
JOIN  (SELECT cntr_value,OBJECT_NAME
    FROM sys.dm_os_performance_counters 
    WHERE counter_name = 'Buffer cache hit ratio base'
        AND OBJECT_NAME = 'SQLServer:Buffer Manager') b ON  a.OBJECT_NAME = b.OBJECT_NAME
WHERE a.counter_name = 'Buffer cache hit ratio'
AND a.OBJECT_NAME = 'SQLServer:Buffer Manager'

MS SQL Server Queries

Query to find Auto Shrink Status

Open SSMS ( SQL SERVER MANAGEMENT STUDIO ) ==> Right click on the instance ==> New Query and paste the below code to find the Auto Shrink status


SELECT [name] AS DatabaseName
, CONVERT(varchar(10),DATABASEPROPERTYEX([Name] , 'IsAutoClose')) AS AutoClose
, CONVERT(varchar(10),DATABASEPROPERTYEX([Name] , 'IsAutoShrink')) AS AutoShrink
FROM master.dbo.sysdatabases
Order By DatabaseName 

Learn SQL DBA Online

Learn SQL Online

Important Quires

SQL Versions


RTM (no SP) SP1 SP2 SP3 SP4
 SQL Server 2017 14.0.1000.169
     codename vNext *new
 SQL Server 2016 13.0.1601.5 13.0.4001.0
or 13.1.4001.0
 SQL Server 2014 12.0.2000.8 12.0.4100.1 12.0.5000.0
or 12.1.4100.1 or 12.2.5000.0
 SQL Server 2012 11.0.2100.60 11.0.3000.0 11.0.5058.0 11.0.6020.0 11.0.7001.0
     codename Denali or 11.1.3000.0 or 11.2.5058.0 or 11.3.6020.0 or 11.4.7001.0
 SQL Server 2008 R2 10.50.1600.1 10.50.2500.0 10.50.4000.0 10.50.6000.34
     codename Kilimanjaro or 10.51.2500.0 or 10.52.4000.0 or 10.53.6000.34
 SQL Server 2008 10.0.1600.22 10.0.2531.0 10.0.4000.0 10.0.5500.0 10.0.6000.29
     codename Katmai or 10.1.2531.0 or 10.2.4000.0 or 10.3.5500.0 or 10.4.6000.29
 SQL Server 2005 9.0.1399.06 9.0.2047 9.0.3042 9.0.4035 9.0.5000
     codename Yukon
 SQL Server 2000 8.0.194 8.0.384 8.0.532 8.0.760 8.0.2039
     codename Shiloh
 SQL Server 7.0 7.0.623 7.0.699 7.0.842 7.0.961 7.0.1063
     codename Sphinx

Share

Fixing a database with a high VLF count

Fixing a database with a high VLF count is a simple process: 1. Check the current size of the transaction log. 2. Backup the transacti...