help! my sql server log file is too big!!! tech republic

5
Help! My SQL Server Log File is too big!!! | TechRepublic http://www.techrepublic.com/blog/datacenter/help-my-sql-server-log-file-is-too-big/448[08/29/2012 3:44:00 PM] Blogs Downloads Newsletters Galleries Q&A Discussions News Research Library Home / Blogs / The Enterprise Cloud The Enterprise Cloud Help! My SQL Server Log File is too big!!! By Tim Chapman September 22, 2008, 1:26 PM PDT Takeaway: Overgrown transactional log files can turn into real problems if they are not handled properly. Today SQL Server consultant Tim Chapman discusses the perils of not handling SQL Server log growth properly, and what can be done to correct the problems. Over the years, I have assisted so many different clients whose transactional log file has become “too large” that I thought it would be helpful to write about it. The issue can be a system crippling problem, but can be easily avoided. Today I’ll look at what causes your transaction logs to grow too large, and what you can do to curb the problem. Note: For the purposes of today’s article, I will assume that you’re using SQL Server 2005 or later. Every SQL Server database has at least two files; a data file and a transaction log file. The data file stores user and system data while the transaction log file stores all transactions and database modifications made by those transactions. As time passes, more and more database transactions occur and the transaction log needs to be maintained. If your database is in the Simple recovery mode, then the transaction log is truncated of inactive transaction after the Checkpoint process occurs. The Checkpoint process writes all modified data pages from memory to disk. When the Checkpoint is performed, the inactive portion of the transaction log is marked as reusable. Transaction Log Backups If your database recovery model is set to Full or Bulk-Logged, then it is absolutely VITAL that you make transaction log backups to go along with your full backups. SQL Server 2005 databases are set to the Full recovery model by default, so you may need to start creating log backups even if you haven’t ran into problems yet. The following query can be used to determine the recovery model of the databases on your SQL Server instance. SELECT name, recovery_model_desc FROM sys.databases Before going into the importance of Transactional Log Backups, I must criticize the importance of creating Full database backups. If you are not currently creating Full database backups and your database contains data that you cannot afford to lose, you absolutely need to start. Full backups are the starting point for any type of recovery process, and are critical to have in case you run into trouble. In fact, you cannot create transactional log backups without first having created a full backup at some point. Follow this blog: IT Management Development IT Support Data Center Networks Security Log In Join TechRepublic FAQ Go Pro! ZDNet Asia SmartPlanet TechRepublic

Upload: kaing-menglieng

Post on 15-Jan-2017

382 views

Category:

Documents


0 download

TRANSCRIPT

  • Help! My SQL Server Log File is too big!!! | TechRepublic

    http://www.techrepublic.com/blog/datacenter/help-my-sql-server-log-file-is-too-big/448[08/29/2012 3:44:00 PM]

    Blogs Downloads Newsletters Galleries Q&A Discussions News

    Research Library

    Home / Blogs / The Enterprise Cloud

    The Enterprise Cloud

    Help! My SQL Server Log Fileis too big!!!By Tim ChapmanSeptember 22, 2008, 1:26 PM PDT

    Takeaway: Overgrown transactional log files can turn into real problems if they are not handledproperly. Today SQL Server consultant Tim Chapman discusses the perils of not handling SQLServer log growth properly, and what can be done to correct the problems.

    Over the years, I have assisted so many different clients whose transactional log file has becometoo large that I thought it would be helpful to write about it. The issue can be a system cripplingproblem, but can be easily avoided. Today Ill look at what causes your transaction logs to growtoo large, and what you can do to curb the problem.

    Note: For the purposes of todays article, I will assume that youre using SQL Server 2005 orlater.

    Every SQL Server database has at least two files; a data file and a transaction log file. The datafile stores user and system data while the transaction log file stores all transactions and databasemodifications made by those transactions. As time passes, more and more database transactionsoccur and the transaction log needs to be maintained. If your database is in the Simple recoverymode, then the transaction log is truncated of inactive transaction after the Checkpoint processoccurs. The Checkpoint process writes all modified data pages from memory to disk. When theCheckpoint is performed, the inactive portion of the transaction log is marked as reusable.

    Transaction Log BackupsIf your database recovery model is set to Full or Bulk-Logged, then it is absolutely VITAL that youmake transaction log backups to go along with your full backups. SQL Server 2005 databases areset to the Full recovery model by default, so you may need to start creating log backups even ifyou havent ran into problems yet. The following query can be used to determine the recoverymodel of the databases on your SQL Server instance.

    SELECT name, recovery_model_desc

    FROM sys.databases

    Before going into the importance of Transactional Log Backups, I must criticize the importance ofcreating Full database backups. If you are not currently creating Full database backups and yourdatabase contains data that you cannot afford to lose, you absolutely need to start. Full backupsare the starting point for any type of recovery process, and are critical to have in case you runinto trouble. In fact, you cannot create transactional log backups without first having created a fullbackup at some point.

    Follow this blog:

    IT Management Development IT Support Data Center Networks Security

    Log In Join TechRepublic FAQ Go Pro!ZDNet Asia SmartPlanet TechRepublic

    http://ad.doubleclick.net/click;h=v8/3ce0/0/0/%2a/d;44306;0-0;0;74341537;31-1/1;0/0/0;;~sscs=%3fhttp://www.techrepublic.com/http://www.techrepublic.com/http://www.techrepublic.com/blogshttp://www.techrepublic.com/downloadshttp://www.techrepublic.com/newslettershttp://www.techrepublic.com/photoshttp://www.techrepublic.com/forum/questionshttp://www.techrepublic.com/forum/discussionshttp://www.techrepublic.com/newshttp://www.techrepublic.com/research-libraryhttp://www.techrepublic.com/http://www.techrepublic.com/blogshttp://www.techrepublic.com/blog/datacenterhttp://ad.doubleclick.net/click;h=v8/3ce0/0/0/%2a/j;44306;0-0;0;74341537;3823-300/100;0/0/0;;~sscs=%3fhttp://www.techrepublic.com/search?q=tim+chapmanhttp://www.techrepublic.com/blog/datacenter?mode=rsshttp://www.techrepublic.com/alerts/add?url=http%3A%2F%2Fwww.techrepublic.com%2Fblog%2Fdatacenter%3Fmode%3Drss&title=The+Enterprise+Cloud+on+TechRepublic&source=http%3A%2F%2Fwww.techrepublic.com%2Fblog%2Fdatacenter%2Fhelp-my-sql-server-log-file-is-too-big%2F448&frequency=weekly&rss_type=38http://www.techrepublic.com/members/login?regSrc=global-reghttp://www.techrepublic.com/members/join?regSrc=global-reghttp://www.techrepublic.com/faq/generalhttp://www.techrepublic.com/prohttp://www.zdnetasia.com/http://www.smartplanet.com/http://www.techrepublic.com/

  • Help! My SQL Server Log File is too big!!! | TechRepublic

    http://www.techrepublic.com/blog/datacenter/help-my-sql-server-log-file-is-too-big/448[08/29/2012 3:44:00 PM]

    The Full or Bulk-logged Recovery ModeWith the Full or Bulk-Logged recovery mode, inactive transactions remain in the transaction log fileuntil after a Checkpoint is processed and a transaction log backup is made. Note that a fullbackup does not remove inactive transactions from the transaction log. The transaction logbackup performs a truncation of the inactive portion of the transaction log, allowing it to be reusedfor future transactions. This truncation does not shrink the file, it only allows the space in the file tobe reused (more on file shrinking a bit later). It is these transaction log backups that keep yourtransaction log file from growing too large. An easy way to make consistent transaction logbackups is to include them as part of your database maintenance plan.

    If your database recovery model is set to FULL, and youre not creating transaction log backupsand never have, you may want to consider switching your recovery mode to Simple. The Simplerecovery mode should take care of most of your transaction log growth problems because the logtruncation occurs after the Checkpoint process. Youll not be able to recover your database to apoint in time using Simple, but if you werent creating transactional log backups to begin with,restoring to a point in time wouldnt have been possible anyway. To switch your recovery model toSimple mode, issue the following statement in your database.

    ALTER DATABASE YourDatabaseName

    SET RECOVERY SIMPLE

    Not performing transaction log backups is probably the main cause for your transaction loggrowing too large. However, there are other situations that prevent inactive transactions frombeing removed even if youre creating regular log backups. The following query can be used toget an idea of what might be preventing your transaction log from being truncated.

    SELECT name, log_reuse_wait_desc

    FROM sys.databases

    Long-Running Active TransactionsA long running transaction can prevent transaction log truncation. These types of transactions canrange from transactions being blocked from completing to open transactions waiting for user input. In any case, the transaction ensures that the log remain active from the start of the transaction. The longer the transaction remains open, the larger the transaction log can grow. To see thelongest running transaction on your SQL Server instance, run the following statement.

    DBCC OPENTRAN

    If there are open transactions, DBCC OPENTRAN will provide a session_id (SPID) of theconnection that has the transaction open. You can pass this session_id to sp_who2 to determinewhich user has the connection open.

    EXECUTE sp_who2 spid

    Alternatively, you can run the following query to determine the user.

    SELECT * FROM sys.dm_exec_sessions

    WHERE session_id = spid from DBCC OPENTRAN

    You can determine the SQL statement being executed inside the transactions a couple of differentways. First, you can use the DBCC INPUTBUFFER() statement to return the first part of the SQLstatement

    DBCC INPUTBUFFER(spid) from DBCC OPENTRAN

    Alternatively, you can use a dynamic management view included in SQL Server 2005 to return theSQL statement:

    SELECT r.session_id, r.blocking_session_id, s.program_name, s.host_name, t.textFROM sys.dm_exec_requests r INNER JOIN sys.dm_exec_sessions s ON r.session_id =

    http://blogs.techrepublic.com.com/datacenter/?p=248http://articles.techrepublic.com.com/5100-10878_11-6088055.htmlhttp://articles.techrepublic.com.com/5100-10878_11-6120976.html#

  • Help! My SQL Server Log File is too big!!! | TechRepublic

    http://www.techrepublic.com/blog/datacenter/help-my-sql-server-log-file-is-too-big/448[08/29/2012 3:44:00 PM]

    s.session_id CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t WHERE s.is_user_process = 1 AND r.session_id = SPID FROM DBCC OPENTRAN

    BackupsLog truncation cannot occur during a backup or restore operation. In SQL Server 2005 and later,you can create a transaction log backup while a full or differential backup is occurring, but the logbackup will not truncate the log due to the fact that the entire transaction log needs to remainavailable to the backup operation. If a database backup is keeping your log from being truncatedyou might consider cancelling the backup to relieve the immediate problem.

    Transactional ReplicationWith transactional replication, the inactive portion of the transaction log is not truncated untiltransactions have been replicated to the distributor. This may be due to the fact that thedistributor is overloaded and having problems accepting these transactions or maybe because theLog Reader agent should be ran more often. IF DBCC OPENTRAN indicates that your oldestactive transaction is a replicated one and it has been open for a significant amount of time, thismay be your problem.

    Database MirroringDatabase mirroring is somewhat similar to transactional replication in that it requires that thetransactions remain in the log until the record has been written to disk on the mirror server. If themirror server instance falls behind the principal server instance, the amount of active log space willgrow. In this case, you may need to stop database mirroring, take a log backup that truncates thelog, apply that log backup to the mirror database and restart mirroring.

    Disk SpaceIt is possible that youre just running out of disk space and it is causing your transaction log toerror. You might be able to free disk space on the disk drive that contains the transaction log filefor the database by deleting or moving other files. The freed disk space will allow for the log file toenlarge. If you cannot free enough disk space on the drive that currently contains the log file thenyou may need to move the file to a drive with enough space to handle the log. If your log file isnot set to grow automatically, youll want to consider changing that or adding additional space tothe file. Another option is to create a new log file for the database on a different disk that hasenough space by using the ALTER DATABASE YourDatabaseName ADD LOG FILE syntax.

    Shrinking the FileOnce you have identified your problem and have been able to truncate your log file, you mayneed to shrink the file back to a manageable size. You should avoid shrinking your files on aconsistent basis as it can lead to fragmentation issues. However, if youve performed a logtruncation and need your log file to be smaller, youre going to need to shrink your log file. Youcan do it through management studio by right clicking the database, selecting All Tasks, Shrink,then choose Database or Files. If I am using the Management Studio interface, I generally selectFiles and shrink only the log file.

    This can also be done using TSQL. The following query will find the name of my log file. Ill needthis to pass to the DBCC SHRINKFILE command.

    SELECT name

    FROM sys.database_files

    WHERE type_desc = LOG

    Once I have my log file name, I can use the DBCC command to shrink the file. In the followingcommand I try to shrink my log file down to 1GB.

    DBCC SHRINKFILE (SalesHistory_Log, 1000)

    Also, make sure that your databases are NOT set to auto-shrink. Databases that are shrank atcontinuous intervals can encounter real performance problems.

    TRUNCATE_ONLY and NOLOG

  • Help! My SQL Server Log File is too big!!! | TechRepublic

    http://www.techrepublic.com/blog/datacenter/help-my-sql-server-log-file-is-too-big/448[08/29/2012 3:44:00 PM]

    9Comments

    Add Your Opinion

    See All CommentsMost Recent My Contacts

    Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublics freenewsletters.

    About Tim Chapman

    If youre a DBA and have ran into one of the problems listed in this article before, you might beasking yourself why I havent mentioned just using TRUNCATE_ONLY to truncate the log directlywithout creating the log backup. The reason is that in almost all circumstances you should avoiddoing it. Doing so breaks the transaction log chain, which makes recovering to a point in timeimpossible because you have lost transactions that have occurred not only since the lasttransaction log backup but will not able to recovery any future transactions that occur until adifferential or full database backup has been created. This method is so discouraged thatMicrosoft is not including it in SQL Server 2008 and future versions of the product. Ill include thesyntax here to be thorough, but you should try to avoid using it at all costs.

    BACKUP LOG SalesHistory

    WITH TRUNCATE_ONLY

    It is just as easy to perform the following BACKUP LOG statement to actually create the logbackup to disk.

    BACKUP LOG SalesHistory

    TO DISK = C:/SalesHistoryLog.bak

    Moving forwardToday I took a look at several different things that can cause your transaction log file to becometoo large and some ideas as to how to overcome your problems. These solutions range fromcorrecting your code so that transactions do not remain open so long, to creating more frequentlog backups. In additional to these solutions, you should also consider adding notifications to yoursystem to let you know when your database files are reaching a certain threshold. The moreproactive you are in terms of alerts for these types of events, the better chance youll have tocorrect the issue before it turns into a real problem.

    ================================================

    TechRepublics Servers and Storage newsletter, delivered on Monday andWednesday, offers tips, in-depth articles, and downloads to help you manageand optimize your data center. Automatically sign up today!

    Join the conversation!

    Full Bio Contact

    Microsoft Small BusinessServer 2008 and WindowsEssential Business Servercompared

    Installing a simple Webserver on Windows Server2008 from a script

    Follow via:

    Staff Picks Top Rated

    http://www.techrepublic.com/forum/discussions/102-303210http://www.techrepublic.com/newslettershttp://www.techrepublic.com/newslettershttp://nl.com.com/MiniFormHandler?brand=techrepublic&list_id=e040http://www.techrepublic.com/blog/datacenter/microsoft-small-business-server-2008-and-windows-essential-business-server-compared/447http://www.techrepublic.com/blog/datacenter/microsoft-small-business-server-2008-and-windows-essential-business-server-compared/447http://www.techrepublic.com/blog/datacenter/microsoft-small-business-server-2008-and-windows-essential-business-server-compared/447http://www.techrepublic.com/blog/datacenter/microsoft-small-business-server-2008-and-windows-essential-business-server-compared/447http://www.techrepublic.com/blog/datacenter/microsoft-small-business-server-2008-and-windows-essential-business-server-compared/447http://www.techrepublic.com/blog/datacenter/installing-a-simple-web-server-on-windows-server-2008-from-a-script/442http://www.techrepublic.com/blog/datacenter/installing-a-simple-web-server-on-windows-server-2008-from-a-script/442http://www.techrepublic.com/blog/datacenter/installing-a-simple-web-server-on-windows-server-2008-from-a-script/442http://www.techrepublic.com/blog/datacenter/installing-a-simple-web-server-on-windows-server-2008-from-a-script/442http://www.techrepublic.com/forum/discussions/102-303210/rsshttp://www.techrepublic.com/alerts/add?url=http%3A%2F%2Fwww.techrepublic.com%2Fforum%2Fdiscussions%2F102-303210%2Frss&title=Discussion+on+Help%21++My+SQL+Server+Log+File+is+too+big%21%21%21+on+TechRepublic&source=http%3A%2F%2Fwww.techrepublic.com%2Fblog%2Fdatacenter%2Fhelp-my-sql-server-log-file-is-too-big%2F448&frequency=weekly&rss_type=42

  • Help! My SQL Server Log File is too big!!! | TechRepublic

    http://www.techrepublic.com/blog/datacenter/help-my-sql-server-log-file-is-too-big/448[08/29/2012 3:44:00 PM]

    Join Login

    See all comments

    Join the TechRepublic Community and join the conversation! Signing-up isfree and quick, Do it now, we want to hear your opinion.

    Thanks Tim, you saved my life. My 1Gb+ database file had a 228Gb log file on a230Gb hard drive, I was down to my last 10Mb!! 5 minutes after reading yourexcellent article I had a 103Mb log file.... Read Whole Comment +

    View in thread

    RE: Help! My SQL Server Log File is too big!!!Russ P 26th Jun 2010

    2Votes

    http://www.techrepublic.com/forum/discussions/102-303210http://www.techrepublic.com/members/join?regSrc=disc-starthttp://www.techrepublic.com/members/join?regSrc=disc-starthttp://www.techrepublic.com/members/login?regSrc=disc-starthttp://www.techrepublic.com/members/login?regSrc=disc-starthttp://www.techrepublic.com/forum/discussions/102-303210http://www.techrepublic.com/forum/discussions/102-303210-3315398http://www.techrepublic.com/members/profile/6468140http://www.techrepublic.com/util/votehttp://www.techrepublic.com/util/vote

    techrepublic.comHelp! My SQL Server Log File is too big!!! | TechRepublic

    9nLWZpbGUtaXMtdG9vLWJpZy80NDgA: form1: q: button3: