SQL Server – How to Clear or Empty Transaction Log Space Used in Percent

sql servert-sqltransaction-log

I am trying to learn more about the transaction log, which I think is the bottleneck of many Stored Procedures (used for reporting) which do data modifications.

So I tried below:

select * from sys.dm_db_log_space_usage;
update dbo.A
set col1 = 'hello'
where id < 500000;
select * from sys.dm_db_log_space_usage;

So now the log file used is 1.8%. How can I clear it? I thought when the transaction is committed it resets to "zero" (and by "it" I mean percent of transaction log used). I have tried `checkpoint' but that did not help.

Best Answer

The log file is divided into several Virtual Log Files internally. The percent usage is based on this. If you have, say, 4 VLFs of equal size, you will never have less than 25% usage. There is always the head of the log and that VLF is in use.

The percentage can be different even when only one VLF is used, since VLFs in the same ldf file can be of different sizes.

DBCC LOGINFO will tell you which VLFs are in use. Status column. 2 means in use, 0 means not in use.

If you have more than one VLF in use, then the log_reuse_wait_desc column in sys,databases will tell you why SQL Server has more than one in use. Is it waiting for a log backup? Is replication holding it up? Etc.