Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 How to "defrag" VLFs?

Author  Topic 

Kristen
Test

22859 Posts

Posted - 2010-06-02 : 20:19:31
I've got a large database (for me) which has been running for 10 years, and in that time it has grown bit-by-bit.

Just restored it onto SQL 2008 as part of migration testing and DBCC LOGINFO() has 1,759 entries.

MDF is 10GB, LDF is 20GB - but I think that needs sorting out / shrinking as I doubt that is "normal size"

How do I reduce the VLFs? as right now I have a golden opportunity to do so, and probably won't have again for another 10 years!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-06-02 : 20:53:16
Check this one out:

http://www.sqlskills.com/blogs/kimberly/post/8-Steps-to-better-Transaction-Log-throughput.aspx

There are a few links in there you should also read. You should try to figure out how many VLFs you think you need before shrinking it.

edit: you should also check these out:

http://www2.sqlblog.com/blogs/linchi_shea/archive/2009/02/09/performance-impact-a-large-number-of-virtual-log-files-part-i.aspx
http://www2.sqlblog.com/blogs/linchi_shea/archive/2009/02/12/performance-impact-a-large-number-of-virtual-log-files-part-ii.aspx
http://www.karaszi.com/SQLServer/info_dont_shrink.asp

The last one has some code that helps to cycle the active portion of the log so you can shrink it effectively.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-02 : 21:12:50
I recently did this on a mission critical 1TB database (200GB log) as crash recovery took 4-5 minutes recently and found it was due to the amount of VLFs. I thought I had fixed the VLFs last year, but a MS engineer says that my shrink must not have worked. When you run DBCC SHRINKFILE, you actually have to confirm that the file size went down as it won't error even if it doesn't shrink it. So anyway, I had to do it again a couple of weeks ago. Last year I did change the growth setting to 8GB and that change stuck unlike the shrinks.

My process to fix this problem was to repeatedly shrink it down in 1-2GB chunks. I frequently ran tlog backups during the process. I got it all the way down to 512MB, although the MS engineer would have preferred a smaller number. I then grew it out to 50GB in 8GB increments. The MS engineer says he would have preferred 16GB segments for a database as large as ours. I used 8GB per the recommendation I heard last year from another DBA that experienced a production outage due to too many VLFs.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-02 : 21:13:39
By the way, I did this during primetime with no effect on the database or application. The frequent tlog backups helped to eliminate the impact.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-06-03 : 04:50:42
Does it matter that the first 4 are small? I presume those are the ones left behind when I did the SHRINK

FileId FileSize StartOffset FSeqNo Status Parity CreateLSN
----------- -------------------- -------------------- ----------- ----------- ------ ---------------------------
2 253952 8192 0 0 128 0
2 253952 262144 0 0 64 0
2 253952 516096 486967 2 128 0
2 278528 770048 0 0 128 0
2 524222464 1048576 0 0 0 486967000000005700001
2 524222464 525271040 0 0 0 486967000000005700001
etc. rest all "large"
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-03 : 11:16:28
4 small ones are fine.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-06-03 : 12:09:28
Thanks Tara.

For anyone else coming along laster here's what I did - based on the links posted above:

USE MyDatabaseName
GO

-- Check number of VLFs
DBCC LOGINFO()

-- Backup the LOG
BACKUP LOG MyDatabaseName TO DeviceName

-- Get name of TLog Logical Filename (NAME column)
EXEC sp_helpfile

-- Shrink the log to as small as possible
DBCC SHRINKFILE(MyDatabaseName_log, TRUNCATEONLY)

-- Re Check number of VLFs - should be only 4 I think
DBCC LOGINFO()

-- Alter the database to modify the transaction log file to the appropriate size - in one step
-- NOTE: For larger than 8GB create slightly smaller than 8GB and extend in 8GB chunks
-- NOTE *** HOWEVER *** Do not use an exact multiple of 4GB (Bug issue)

ALTER DATABASE MyDatabaseName
MODIFY FILE
(
NAME = MyDatabaseName_log
, SIZE = NewTotalSize -- Whole number only, default is MB, or suffix with KB, MB, GB, TB
, MAXSIZE = UNLIMITED
, FILEGROWTH = 100MB

)

e.g. use 8000MB first time, to get to just short of 8GB, an then 10000MB second time - to extend that up to just short of 10GB (my desired final size)

-- Final check that there are few enough VLFs
DBCC LOGINFO()
Go to Top of Page
   

- Advertisement -