jump to navigation

Looking into the DBCC LOGINFO and DBCC SQLPERF commands December 28, 2012

Posted by fofo in Sql Server, Sql Server 2005, SQL Server 2008, SQL Server 2012.
Tags: , ,
trackback

In this post I would like to talk a bit about Transaction Log,its various parts and its architecture. I will not go into details about what Transaction Log is because it is well documented elsewhere. I would like also to highlight a few important points. Ι will provide hands-on examples for the DBCC LOGINFO and DBCC SQLPERF commands.

I have been helping out a friend to understand various important issues regarding Transaction Logs in SQL Server databases.The transaction log records all the transactions and the modifications on the database those transactions have. Usually you have one transaction log file per database but you can have multiple files.The default size of a transaction log (if not specified) is the 25% of the total size of all data files created for the database.It is always a good idea to have the size of the transaction log planned beforehand.Bear in mind that the transaction log is zero initialized and that is a costly operation in both time and resources.The transaction log is divided into smaller parts that are called VLFs (Virtual Log Files). It is easier to manage the transaction log like that.  You have a active and inactive VLFs . One VLF must be active at any time in the database. You cannot configure the number and the size of the VLFs in the transaction log. As transaction log grows, new VLFs are created and their size is determined by SQL Server. There is a formula that determines the number and the size of the VLFs. If the transaction log grows by 64 MBytes, 4 new VLFs are created each 1/4 of the growth size.If the transaction log grows by 64 MBytes to 1Gbytes, 8 new VLFs are created each 1/8 of the growth size.If the transaction log grows by  1Gbytes or more, 16 new VLFs are created each 1/16 of the growth size.Also bear in mind that each VLF is uniquely identified by a sequence number.

Let’s have a look at the actual hands-on demos.

I will be using SQL Server 2012 Εnterprise  edition in this post but feel free to use SQL Server 2008/R2 versions and any other edition you might have installed (Standard, Express e.t.c).

1) Launch SSMS and connect to the local (or any other instance) of SQL Server

2) I will be looking firstly at the DBCC LOGINFO command which provides us with important information about the structure of the transaction log.

3) In a new query window in SSMS , type (or copy paste) the following t-sql script and then execute it


USE MASTER;
GO

CREATE DATABASE [Football] ON  PRIMARY
( NAME = N'Football_Data', FILENAME = N'D:\DATA\Football_Data.mdf' , SIZE = 180MB , MAXSIZE = UNLIMITED, FILEGROWTH = 20MB )
LOG ON
( NAME = N'Football_Log', FILENAME = N'D:\DATA\Football_log.ldf' , SIZE = 40MB , MAXSIZE = UNLIMITED, FILEGROWTH = 20MB)
GO

DBCC LOGINFO(N'Football');
GO

Let me explain what I do in this example. I create a database Football.Then I run the DBCC LOGINFO(N’Football’) to understand a bit about the log structure and the number of VLFs

Have a look at the photo below

loginfo

As you can see there are 4 VLFs.Each one of them is 10 Mbytes.So we can see that the formula applies in this case. The Log file size is 40 Mbytes so we expect to have 4 VLFs.

4) Now let’s have a look at the DBCC SQLPERF(LOGSPACE) command. This command provides transaction log space usage statistics for all databases. Have a look

In a new query window type and execute the following T-Sql command.


USE MASTER;
GO

DBCC SQLPERF(LOGSPACE);
GO

Have a look at the picture below

sqlperf-1

5) Now I am going to increase the size of the transaction log by inserting sample data in the database and then check again the number and the size of the VLFs in the transaction log. I will also check the transaction log file size and the part of it that is used.

Type (copy and paste) the following T-SQL script. Execute it



USE FOOTBALL;
GO

ALTER DATABASE Football MODIFY FILE (NAME =N'Football_Log',SIZE=60MB);

DBCC LOGINFO(N'Football');
GO

DBCC SQLPERF(LOGSPACE);
GO

6) We add more space in the transaction log and then execute the DBCC LOGINFO and DBCC SQLPERF commands. Have a look at the picture below

sqladdlog

We can see now that the size of the transaction log is more than 64Mbytes we have 8 VLFs

7) Now in a new query window type (copy paste) and execute the following script.


USE FOOTBALL;
GO

CREATE TABLE Footballer
(
FootballerID int identity(1,1),
Footballer_No varchar(20),
Footballer_lastname nvarchar(50),
Footballer_firstname nvarchar(50),
Footballer_middlename nvarchar(50)
)
GO

CREATE UNIQUE CLUSTERED INDEX cx_Foo_IX ON Footballer (FootballerID)
GO

declare @CTR INT
declare @ctrstr varchar(7)
select @ctr=0
WHILE @CTR<100.000
BEGIN
select @ctr=@ctr+1
select @ctrstr=Str(@ctr)
insert footballer(Footballer_No ,Footballer_lastname,Footballer_firstname, Footballer_middlename) values('F_No'+@ctrstr,'Footlname'+@ctrstr,'Footfname'+@ctrstr,'Footmname'+@ctrstr)
END

SELECT * FROM Footballer;
GO


I create a table in the database.Then I create a clustered index and insert 100.000 rows in it. That will increase the size of the used space in the transaction log.

Type and execute the T-SQL code below.

DBCC LOGINFO(N'Football');
GO


DBCC SQLPERF(LOGSPACE);
GO


Have a look at the picture below to see results I have got when I run the T-SQL code above.

transaction-log

Hope you learned about transaction log architecture and the importance of DBCC LOGINFO and DBCC SQLPERF commands.

Hope it helps!!!!

Comments»

1. Dot Net Rules : Looking into the DBCC LOGINFO and DBCC SQLPERF commands - December 29, 2012

[…] will provide hands-on examples for the DBCC LOGINFO and DBCC SQLPERF commands. (read more) Share Posted: Σάββατο, 29 Δεκεμβρίου 2012 12:19 πμ από το […]

2. Lamar - August 28, 2014

Hi there! Would you mind if I share your blog with my facebook group? There’s a lot of folks that I think would really appreciate your content. Please let me know. Thanks

3. Beatriz - September 3, 2014

Hello! I know this is kinda off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!

4. Kraig - September 7, 2014

Every weekend i used to pay a quick visit this website, for the reason that i wish for enjoyment, since this this web site conations truly good funny information too.

5. Flynn - September 7, 2014

Great site. Plenty of helpful info here. I’m sending it to several pals ans additionally sharing in delicious. And obviously, thank you for your sweat!

6. Felicitas - September 17, 2014

Thanks for finally talking about >Looking into the DBCC LOGINFO and DBCC SQLPERF commands | DOT NET RULES <Loved it!

7. UNDERSTANDING THE TRANSACTION LOG 2 | sqlbitsandpieces - October 11, 2014

[…] this post you will find some examples on two interesting commands: DBCC LOGINFO returning a row for each VLF […]

8. Saketh - July 11, 2016

Good Information and very ease understanding, But you have increased the log file from 40 MB to 60 MB that means you have added 20 MB, it is in the band 64 MB and no 8 VLF’s are added please check and correct the post, already there are 4 VLF’s when log was created so only 4 VLF’s are added.

Please refer this link : https://blogs.msdn.microsoft.com/mlavery/2013/04/22/tsql-to-identify-databases-with-high-number-of-vlfs/


Leave a comment