Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


June 08, 2009

Do You Need a Shrink?

A Windows IT pro gets help dealing with his irrational fear of SQL Server
RSS
Subscribe to Windows IT Pro | See More Performance Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
back to blog index

Dave: So, Doctor, I could do this every night?
Doctor: No not really. Remember that SQL Server likes head room, and if you lowered the ceilings every night, then the transaction log would have to grow on the physical disk every morning. Often a VAR will deploy SQL Server and leave the databases in a "mode" called simple.
Dave: I like "simple."
Doctor: Well, maybe not. If you lost all the input from your users today and you had only a backup from last night, you wouldn't be able to restore the transactions for the data to the last backup.That's why some IT shops back up the transaction logs every few minutes and restore them to another SQL Server as a stand-by SQL Server.
Dave: This is starting to validate my fear.
Doctor: Well, with simple mode, the transaction log growth is restricted. Notice how I have this set up here, if you right-click on the database and choose Properties, then Options.

We can hit the drop-down arrow and choose full recovery. Now before transaction backups will work, you have to back up the MDF file first with a full backup. Then transaction log backups can be done. But now comes your responsibility.

You see, Dave, in full recovery, each data-change operation is recorded in the log. Now your databases are in full recovery mode, so the log files will grow and, if not truncated, will fill up your drive. At that point, SQL Server will stop working—which would, I'm afraid, cause panic!

But there are more geeky details. You see, we can reduce the risk by using a feature called autogrow. To enable autogrow on your database give the logs file a reasonable startup size. Then select the check box for autogrowth and enter a percentage. You could give it a maximum size, but when the log fills up, the database won't work.

Dave: Yikes! That's as bad as the monster underneath my bed!
Doctor: Yes, well perhaps next week we can address that. So we use this feature to allow the database to give itself the head room it needs to keep working. Sure, it uses system resources like disk, CPU, and RAM, but you can sleep at night.

Dave: So how do I get these transaction files under control now?
Doctor: Well, now, we use that T-SQL code I spoke about, and you'll drive.
Dave: Me?
Doctor: Yes, Dave. I have complete confidence in the mission, I mean, you.
Dave: Say what? 
Doctor: Sorry. I've been stuck with that phobia since 2001.
Dave: I guess everyone has them.
Doctor: Yes, once in a while I space out.

Dave: About the transaction logs.
Doctor: Oh yes, open up a query window by hitting that big New Query button. Now let's do a smaller one so it won't take much time, and you can do the rest of them yourself.

I see a database called Probar on your server. First, let's have a look at the MDF and LDF file sizes. Type the following in the query window:

Use Probar
select * from sysfiles

Now highlight them, right-click, and choose Execute.

You'll notice the results below.

The files can be identified by fileID, and each file has a number, so you could use that number to refer to the file later. Now let's back up the file, but we are only going to truncate it with this backup.

We will type:

Backup log probar to disk = 'g:\probar.trn , TRUNCATEONLY'

Now, let's highlight that again and execute. Notice that I didn't back up to your C partition, but I saw that you had a 500GB partition on your server. Notice that I can specify the path in the T-SQL statement to either a local drive on the server or another server or device that I have access to. Why did the consultant from the VAR use that drive?

Dave: Drive blindness, I guess. Kind of like refrigerator blindness.
Doctor: So notice the results in the lower pane.

Processed 108774 pages for database 'probar', file 'ProBar_Log' on file 1.
BACKUP LOG successfully processed 108774 pages in 19.653 seconds (45.340 MB/sec).

Doctor: Be sure you give him my number, OK? So now, let's shrink this transaction file that's a bit larger than its MDF file.

We will type:

DBCC SHRINKFILE (2,TRUNCATEONLY)

and execute that. Then we will type:

DBCC SHRINKFILE (2,500)

and execute that.

Notice the results. The size has not changed yet.

Dave: So when does it shrink?
Doctor: You have just a few more steps to get your disk space back. Now we have to do a regular backup of the transaction file. We use a statement like the truncating one:

BACKUP LOG PROBAR TO DISK = 'G:\PROBAR.TRN'

Notice the results:

Processed 789 pages for database 'PROBAR', file 'ProBar_Log' on file 1.
BACKUP LOG successfully processed 789 pages in 0.240 seconds (26.914 MB/sec).

Now let's rerun that statement "DBCC SHRINKFILE (2,500)" and then that Select statement again. Now, Dave, what do you see?


 
Dave: Wow! Down from 109,704 to 128! That's a big reduction. So I can do this to my big transaction logs every night?
Doctor Avatar: Dave, Dave. Baby steps Dave, baby steps. We are just starting to deal with the result of your SQL Server psychoses and the obvious other ones. For now, let's get your disk space back, and then we'll explore your SQL Server or the reasons why your transaction logs have such an inflated view of themselves, in your next visit.

Dave: Doctor Avatar, I still have one big fear: the bill for this visit?
Doctor: Don't worry, Dave. Your subscription to Windows IT Pro magazine has you covered.
Dave: Thanks, Doc!

End of Article

   Previous  1  [2]  Next  


Reader Comments
Very well written Curt and a wonderful analogy. This is a great introduction to SQL Data and Log files. Thank you very much.

chamezzzz June 09, 2009 (Article Rating: )


Truncating logs files is just bad advice, you need to fully understand your recovery model and if log files are to big and you need point in time recovery, just do log backups more frequently. Shrinking file after you have done that will be fine, just leave room for growth so file doesn't have to grow often as that is a performance killer. @SQLGuyChuck

clathrope June 09, 2009 (Article Rating: )


This article is full of incorrect information and poses ideas that extremely dangerous and can result in the loss of data and corruption of the master data file. First you should point out that the TRUNCATE ONLY option for BACKUP LOG is no longer available in SQL 2008, so you can't just dump your databases transactional history. If you are going to do that just set your database to SIMPLE recovery and the log won't grow like it currently is. Unless you follow the TRUNCATE_ONLY backup with a FULL backup, you lost your ability to do a point in time recovery of the database anyway.

If you do happen to shrink the log file, you should resize it immediately back to a size that prevents growth from happening at all to keep from having excessive Virtual Log Files which impact the time to recovery. There is also no background process that runs every 10 seconds to move log records from the log to the main data file. The process you are referring to is called CheckPoint and it sweeps the buffer pool for changes and writes them to stable media, and it runs when the recovery interval is passed, not every 10 seconds.

The only thing this article has done is guarantee that we'll see more problems on the SQL Server forums related to mismanagement of database log files, and why people can't recover their database after doing the TRUNCATE_ONLY backup you recommend.

jmkehayias June 09, 2009 (Article Rating: )


I didn't mention this but I meant to. You should never shut down SQL Server and delete your database log file, or detach the database and regenerate the log file on attach. This can be disasterous in the event that your database does not shutdown cleanly and requires the log to bring the database online in a consistent state. There are plenty of horror stories online of people losing data or their database by doing this very operation. In the best cases the database is brought online, and it has inconsistent data with no real data loss, but loss of data integrity due to uncommitted changes not being able to roll back. In the worst cases you can't get the database online at all and have to go back to the last backup of the database to recover.

jmkehayias June 09, 2009 (Article Rating: )


I'll second the comments above. Hopefully the advice given here was intended to be part of the pun. The shrink in this story should stick to psychology.

tmitch June 09, 2009 (Article Rating: )


Readers, thanks for your feedback. I've notified the author, Curt Spanburgh, and he intends to respond to the comments as soon as he can.

AnneG_editor June 11, 2009 (Article Rating: )


I am not a SQL DB but know enough to call for qualified help when something comes up. However, what I gained from this article was what to do in an “emergency situation” where the hard drive is full. I did not read this as a How To Best Practice... I would not just start flipping settings due to this article, but I thought it was a good lesson for this type of situation and for the thought process involved.

jsclmedave June 11, 2009 (Article Rating: )


It’s true that the No_LOG and Truncate_Only which are synonymous are removed in SQL 2008. And so Microsoft recommends that these options not be used in Development. In my practice I often find the Trucate_Only option being used by VARs I remove these from the maintenance plans I find for obvious reasons. It is the purpose of the article to address a situation that is only to be used when necessary, thus the suppression of the idea of doing this every night. In fact, SQL Books On Line (BOL) even acknowledges situations like this calling them “Very Special Circumstances” The actual quote is: “Use manual log truncation in only very special circumstances, and create backups of the data immediately.” To that end Microsoft maintains a KB on the commands. You can follow the link here at http://support.microsoft.com/kb/907511.
We find that most of our readers are perceptive of these points and are professional in their reponses.

SCG June 11, 2009 (Article Rating: )


Darn this economy!
A shrink who also shrinks SQL Server log files?
What's next?
A (human) driver who also handles communication
between the OS and a device?
And I guess we'll need a truck driver,
now with 64-bit operating systems.

But seriously...

I agree with jsclmedave's and SCG's comments.

I think the article provides us
with a quick last-resort procedure
to help in an awkward SQL Server situation,
which is incorrect disk space provisioning
combined with no downtime allowed.

I really loved this article.
That's edutainment!!!!

dkalemis June 15, 2009 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now





Search We're in IT
 
We're in IT
NOVEMBER 2009
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30      
or

 Recently in We're in IT
Outlook 2010: A One-Hour "First Contact"

Last Comment
Nothing beneficial for most businesses – no reason to upgrade/purchase – Like Vista – all bling ...
(2 Comments)
Do You Need a Shrink?

Last Comment
Darn this economy! A shrink who also shrinks SQL Server log files? What's next? A (human) driver ...
(9 Comments)
"Well, This Is Another Nice Mesh You've Got Me Into"

Last Comment
Thanks Curt. Live mesh is great - combines a number of useful features and is very easy to use. Gre...
(5 Comments)
How to Change the Name of a Certificate Server
Make a Comment
Free Hypervisors Extend Your IT Resources

Last Comment
Great article Dave! I have added some of your very useful observations to my talking points on this...
(3 Comments)

More blogs about technology,
software, and Windows.

Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement