Sei sulla pagina 1di 5

SQL Server Transaction

logarchitecture and Virtual


log files
Transaction log is reusable circular file which captures transaction details to recover
database in disaster. Sql transaction Log file is divided into number of virtual log files (VLF)
to handle active and inactive transaction in the database. Sql server transaction log writes
entries sequentially into the virtual log files one by one. When one file is full, SQL move to
next virtual log file (VLFs) till it reaches to last one. After reaching at the end, it will go back
to first virtual log file which in inactive state and start using again.

Virtual Log file can be either in active or inactive states

What is Active state in Virtual Log ?


The log records that are currently used by sql server database engine, it can
contain more than one virtual log files that we call it as active virtual log file.
The log records may part of active transactions, replication, mirroring or log
shipping. Active virtual log may be part of transaction which is already
completed but not yet written back to the data file.
What is Inactive state in Virtual Log ?
The log records that are written back to data file and also not in use by database
engine is known as inactive virtual log file. The log records may not be part of
active transactions, replication, mirroring or log shipping. Whenever log backed
up in full and bulk logged recovery model, virtual log file becomes inactive.
How to reuse transaction log?
Transaction log is cyclic process of writing log record into virtual log file by SQL
Server, whenever active virtual log file filled up with data it moves to next
inactive virtual log file. In short it needs inactive virtual log file for writing log
records. If all virtual log files are filled up with active data then it will grow
further to create more virtual log file. If it cannot grow then it will throw error
saying transaction log for database is full and transaction will fail.
To find the reason why transaction log cannot be reuse, execute the below query
Select log_reuse_wait_desc from sys.databases where name =
yourdbname

To reuse virtual log files, we have to truncate logs. After truncation it will release
space and mark as inactive virtual log which is ready to use by SQL.

Lets understand with the SQL Server Transaction log architecture diagram,
assuming below is the transaction log file which divided into 5 virtual log files
(v1, v2, v3, v4 and v5). In this V1 and V2 are in inactive state and V3, V4 and V5
are in active state which associated with active transaction in database.

In the above diagram, LB indicates last transaction log backup for the log
records and CKP indicates the last checkpoint for associated log records.

After number of data modification in log records, SQL moves to the last virtual
log file (V5). Now it will look for inactive virtual log file for writing more log
records so it will go back to first inactive virtual log file (V1) and start writing log
records in V1 and V2. Here we can see that V3, V4 and V5 are in still active
state. Now to mark them as inactive, log backup should be run that will release
some space for SQL to reuse them.

Otherwise if log records keep on growing and finished with writing in V1 and V2
virtual log files then it will throw error saying transaction log for database is full.

Understanding

the

VLF(Virtual

Log

File)

A database can have one or more log file. In general there will be only one log file as there is no
performance improvement by having multiple log file. SQL server uses the transaction log in sequential
manner.As the data file divided into pages,log files are divided into virtual log file(VLF).The size of the

VLFs in a log file may not be in equal size. SQL server decide the size and number of VLF in a log file
based on the size of the log file growth as given below.
Growth upto 64 MB
From 64 MB to 1 GB
Larger than 1 GB

= 4 VLF
= 8 VLF
= 16 VLF

Let us create a database with 64 MB initial log size and later increase it to 1 GB. As per above calculation
the log file should have 12 VLFs. 4 VLF based on initial size and 8 VLF due to changing the log size to 1
GB.
USE MASTER;
GO
CREATE DATABASE Mydb
ON
(

NAME = MyDb_dat, FILENAME = 'D:\MyDb\Mydb.mdf',


SIZE = 10MB, MAXSIZE = 3072MB,

FILEGROWTH = 5MB )

LOG ON ( NAME = MyDb_log,FILENAME = 'D:\MyDb\MyDB.ldf',


SIZE = 64MB, MAXSIZE = 2048MB, FILEGROWTH = 5MB ) ;
GO
ALTER DATABASE Mydb
MODIFY FILE ( NAME = MyDb_Log,FILENAME = 'D:\MyDb\MyDB.ldf',

SIZE = 1024MB)

Now Let us see how many VLF got created. To find out the number of VLF in database log file, we can
make use of DBCC Loginfo.
DBCC loginfo('mydb')

The output is given below.

There are 12 records in the output each represent a VLF.Let us try to understand the result
FileId: This is the file id of the log file and will be same for all 12 records as we have only one log file.If we
have multiple log file , we can multiple numbers here
FileSize: This is the size of the VLF. If you look into the first four, have same size except the fourth one.
This because first 8KB of the log file is used for file header. If you add filesize value of first four records
along with 8192(8KB) , you will get 64MB which is the initial size of the log file.
16711680+16711680+16711680+16965632 =67100672+8192 =67108864bytes =64MB
In the same if you add the last 8 records it will account the 960 MB (1024-64) , the growth happened due
to the alter statement.
StartOffSet: This values is also in bytes, and is the sort column of the output. The first VLF alwasy start
from 8192, which is the number of bytes in a page.As mentioned above, the first 8KB is used for file
header and will not store any log.
FSeqNo: The file sequence number indicates the order of usage of the VLFs. The row with the highest
FSeqNo value is the VLF where current log records are being written.FSeqNo values are not consistent. It
will keep changing each time when VLF are getting reused. We will discuss more about this later in this
post. A value of 0 in this column means that this VLF has never been used at all. That is the reason we
have 0 for all records except one where it is currently logging.
Status: Status has two possible values : 0 and 2. A value of 2 means the VLF is not reusable and a value
0 means it can be reused.It will be more clear as we go further.
Parity: Parity has three possible values 0 ,64 and 128. If the VLF is not used yet, it will have a value 0
and will be set to 64 on first use.Every time a VLF is reused, the parity value is switched between 64 and
128.
CreateLSN: The value indicates when the VLF is created or to group the VLF based on the creation. A
values 0 indicates, those VLFs are created as part of database creation. In our case first four records has
a value 0 which indicate these VLFs are created as part of database creation with 64MB log size. The
remaining 8 records has the same value. These VLF are created as part of our alter database statement
to increase the size of the log file from 64 MB to 1024MB
The above output description is referred from Kalen Delaney Blog Post

Now our transaction log will looks like below

Potrebbero piacerti anche