Question

how to shrink the transaction log file

Asked by: manonng

Anyone know how to without destroying the system cut the transactional log down to size in SQL server V7.

manon

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2001-09-11 at 02:42:25ID20181436
Tags

log

,

sql

,

transaction

Topic

MS SQL Server

Participating Experts
9
Points
100
Comments
29

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. shrink a transaction log
    In this instance we are not interested in keeping a copy of the transaction log, but just want to shrink it as effectively as possible. I have found the following two methods is one prefered over another? Method 1.) BACKUP LOG dbname WITH TRUNCATE_ONLY DBCC SHRINKFILE(dbn...
  2. Transaction Log will not shrink.
    USE DWH GO DBCC SHRINKFILE (2,TRUNCATEONLY) GO I checked the sysfiles for the number identifying the log I want to shrink and as you can see from the message below it has picked the correct file (DWH_Log). Is there anything else I need to do to get it to shrink. I run a scr...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: angelIIIPosted on 2001-09-11 at 03:01:33ID: 6473883

SQL:
exec sp_detachdb 'yourdatabase'

OS:
delete the .ldf file(s)

SQL:
exec sp_attachdb 'yourdatabase', 'c:\mssql7\data\yourdatabase.mdf'

If you database has more than 1 datafile (*.mdf), you should make the list (select * from yourdatabase..sysfiles)

Cheers


 

by: angelIIIPosted on 2001-09-11 at 03:04:12ID: 6473889

If you need to keep the system online, you can also do some TRUNCATE LOG statements, and then DBCC ShrinkFile (yourlogfile)...

BTW, if you have set your transaction log to be AUTOGROW, i recommend to give it a fixed size of approx 20-30% of the size of the data files, and then:
# either set the Truncate Log on Checkpoint to true
# use Transacation Log Backup regulary (every hour)

Cheers

 

by: angelIIIPosted on 2001-09-11 at 03:04:49ID: 6473891

.. and of course, disable the autogrow for the transaction log!!!!
Cheers

 

by: nigelrivettPosted on 2001-09-11 at 03:47:54ID: 6474009

I would go for the sp_detach_db sp_attach_single_file_db option.

Make sure you backup the database first.

 

by: mironPosted on 2001-09-11 at 04:06:02ID: 6474057

Both of these methods defeat the meaning of the transaction loggin.

 

by: angelIIIPosted on 2001-09-11 at 04:18:38ID: 6474093

???

The question is to reduce the size, i guess that it has grown to some GB... If you want to reduce the filesize, and then implement the transaction log backup, everything is fine.

Anyway, there are cases where transaction logging is really overhead that can be let aside... For example Datawarehouse database are typical candidates for this.

Cheers

 

by: mironPosted on 2001-09-11 at 04:31:11ID: 6474124

Well, if the log is not needed for SQL 2K you can set to logging to simple recovery, or for SQL Server 7.0 truncate log on check point. But if you have ongoing transaction logging detaching the log file and forcing a new log file to be generated is somewhat means loosing all the logged data. It is not just the size - it is the data that matters. And of course, a used log space is desirable to be under control, but this is the trouble that we are asked to address.

 

by: Jeremy_DPosted on 2001-09-11 at 05:15:55ID: 6474221

> But if you have ongoing transaction logging detaching the log file and forcing a new log file to be generated is somewhat means loosing all the logged data.

No, SQL Server prevents this. You can only detach a database if it's not in use (ie, no open transactions) and all dirty log pages will be flushed to disk before it is detached.

 

by: mironPosted on 2001-09-11 at 05:26:16ID: 6474252

why would you keep transaction logging, if all the purpose of the log in this scenario is to be sacrificed in some ritual fashion to the sp_detach_db.

 

by: angelIIIPosted on 2001-09-11 at 05:34:45ID: 6474273

miron, the sp_detach_db is only used to reduce the size of the transaction log file ONCE, if the file has grown because of the AutoGrow option turned on. If that file is now 10 GB, and the datafiles are only 1 GB, you do the procedure ONCE to cut down the transaction log file to let's say 500 MB max...
After that, you won't use it again...
Of course, a backup should be done just before this operation for security...

Cheers

 

by: mironPosted on 2001-09-11 at 05:49:58ID: 6474320

angellll, I think some different procedure or chain of procedures should exist to enable both, control of log file size and its use as intended, transaction log backup / up to the point in time recovery.

 

by: angelIIIPosted on 2001-09-11 at 05:57:07ID: 6474345

miron, the problem for SQL Server is that if the Autogrow option is enabled, the file will increase, even if the log has been truncated or backed up. That's crap, but's it's like that...
Cheers

 

by: Jeremy_DPosted on 2001-09-11 at 06:13:47ID: 6474417

miron: SQL Server 7.0 is notoriously difficult about shrinking files. Any acceptable (read: immediate) method in existence can be considered a 'hack', just as this one. The sp_detach_db method just happens to be the easiest and quickest one provided you are in a scenario where you can take the database offline for a short while.

Of course the recommended methods are:
1. Don't let the database files grow to large in the first place
2. Use DBCC SHRINKFILE

Obviously it's to late for 1. The problem with 2 is that it does not immidiately shrink the file, but rather markes it for shrinking in the future. Especially in Log-files that have grown far to large the actual shrinking can be quite some time in the future, and is therefor not an acceptable method when you are pressed for disk-space.

 

by: mironPosted on 2001-09-11 at 06:14:33ID: 6474422

angellll, you mean that the whole logging is a crap that has no other use but to decorate SQL Server with some file.
Cheers.

 

by: mironPosted on 2001-09-11 at 06:37:49ID: 6474493

There should be a better way. It would be nice if we could find a better way to do it. Wouldn't it?
Cheers.

 

by: angelIIIPosted on 2001-09-11 at 06:54:30ID: 6474542

miron,
no, of course not. Why do you think i mean this?

My knowledge of the transaction log in SQL Server is this:
The transaction log file is written to using a file pointer, and this pointer moves as there are transaction logged. If this pointer comes to the end of the files, it tries to grow the file, which can be done if the Autogrow option is turned on. If this isn't the case, SQL Server will try to move the pointer to the start of the file, but this could fail if there are still transactions not backed up nor truncated in the beginning of the file.
Now, using the backup transaction log, all committed transaction are marked as cleared in the transaction log file, and the log write pointer can "overwrite" these entries. If the pointer comes to the point where neither the file can be enlarger nor entries can be overwritten, SQL Server will fail to commit any new transactions and SQL code... The option Truncate Log On Checkpoint will clear any committed transaction automatically, so they are cleared as soon as the transaction are committed.
If the server "crashes" or needs to be recovered up to time X, this log files (and the transaction log backups) are NECESSARY, and i don't think is decoration.

The sp_detachdb will ONLY be used to create a new and clean transaction log file ONCE, thus i recommend that a full backup is performed...

If you don't see things the same way, i would like to hear where you disagree with me.

Cheers

Cheers

 

by: Jeremy_DPosted on 2001-09-11 at 07:03:31ID: 6474569

Couldn't have said it better, AngelIII. In addition, SQL Server can only shrink the file by 'cutting off' a part from the end. Ergo, after a DBCC SHRINKFILE command, SQL Server has to wait for the pointer to recycle to the beginning of the file in order to cut off a large portion from the end - unless, of course, the pointer is already far enough to the beginning - which, according to Murphy, is never the case when you actually need the diskspace right away.

 

by: mironPosted on 2001-09-11 at 07:51:24ID: 6474707

I am working with SQL Server 2000 from July 2000, and have seen SQL Server 7.0 only on occasions since than.
I would like to make sure that the DBCC SHRINCKFILE()
can be used with SQL Server 7.0 the same way I use it with SQL Server 2000 to reduce the used logspace and the logfile itself. I am pretty sure it is, but prefer to double check on it before posting a final answer.

Thanks.

 

by: angelIIIPosted on 2001-09-11 at 08:02:54ID: 6474738

As far as i know, SQL 7 works the same way regarding the DBCC shrinkfile.
Now, only if i understand correctly, you want to post an answer to this question? I think, prior to doing that, you should review the EE guidelines about posting comments/answers at the bottom of the page.

Regards

 

by: mironPosted on 2001-09-11 at 09:47:07ID: 6474908

Here is my answer.

this code should shrink the log file size, leave a log backup file, and provide for continuous log backup with zero downtime / disaster recovery path up to the point of failure.

-- begin SQL code.
BACKUP LOG <db_name> TO DISK = 'C:\log'
UPDATE <t_name> set x = y where 0 = 1
UPDATE <t_name> set x = y where 0 = 1
UPDATE <t_name> set x = y where 0 = 1
UPDATE <t_name> set x = y where 0 = 1
UPDATE <t_name> set x = y where 0 = 1
UPDATE <t_name> set x = y where 0 = 1

DBCC SHRINKFILE ( <file_id>, NOTRUNCATE )
DBCC SHRINKFILE( <file_id>, # )
-- where # is desired size of log file
-- end of SQL code.

to locate log file id(s),
-- begin SQL
select fileid
from sysfiles
where FILEPROPERTY( [name], 'IsLogFile') = 1
-- end SQL

 

by: manonngPosted on 2001-09-11 at 10:13:38ID: 6474959

hi

I tries both method, but none of these work!

I have received an error message 'current database is in use' when I was performing the sp_detach_db command, but I sure I have shut down all the related apps.

then I performed the dbcc shrinkfile command, but the file size still remaining the same.

any help?

manon

 

by: setiawanPosted on 2001-09-11 at 11:18:48ID: 6475050

i suspect you are using the database on Query Analyzer, did u ?
sp_detach_db should be able to work...

 

by: angelIIIPosted on 2001-09-11 at 14:31:56ID: 6475296

select * from sysprocesses where dbid=db_id('yourdatabase')

or also the exec sp_who2

will show you all the necessary information who is currently logged in to your database...
CHeers

 

by: mironPosted on 2001-09-11 at 14:35:25ID: 6475300

did you try to run it like this

use master
ALTER DATABASE <db_name> SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE <db_name> SET MULTI_USER WITH ROLLBACK IMMEDIATE
GO
execute sp_detach_db <db_name>


For shrinkfile not working, let me take a look at it. Shrinkfile will not work by itself, the block of code needs to be executed as a whole to make it work.

Thanks.

 

by: astaecPosted on 2002-03-02 at 15:50:46ID: 6836821

Have you been helped here?  Is more needed?
":0)
Asta

 

by: __Holly__Posted on 2002-07-11 at 17:19:22ID: 7147737

yeah all you have to do is to take a backup of the transaction log and it will truncate the log by default

 

by: CleanupPingPosted on 2003-09-03 at 13:03:55ID: 9281601

manonng:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

 

by: arbertPosted on 2003-10-14 at 21:22:36ID: 9552155

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Select AngelIII's comment

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Arbert
EE Cleanup Volunteer

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...