Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
(
FILEGROWTH = 5MB )
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')
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