SQL Server 2008 - Shrinking the Transaction Log - Any way to automate?

后端 未结 6 504
醉酒成梦
醉酒成梦 2021-02-03 14:30

I went in and checked my Transaction log the other day and it was something crazy like 15GB. I ran the following code:

USE mydb
GO
BACKUP LOG mydb WITH TRUNCATE         


        
6条回答
  •  离开以前
    2021-02-03 15:27

    Based on Microsoft recommendation Before you intend to Shrink log file you should first try to perform the following capabilities:

    • Freeing disk space so that the log can automatically grow.
    • Moving the log file to a disk drive with sufficient space.
    • Increasing the size of a log file.
    • Adding a log file on a different disk.
    • Turn on auto growth by using the ALTER DATABASE statement to set a non-zero growth increment for the FILEGROWTH option.

      ALTER DATABASE EmployeeDB MODIFY FILE ( NAME = SharePoint_Config_log, SIZE = 2MB, MAXSIZE = 200MB, FILEGROWTH = 10MB );

    Also, you should be aware of shrink operation via maintenance plan will effect on *.mdf file and *.ldf file. so you need to create a maintenance plan with SQL job task and write the following command to can only shrink *.ldf file to your appropriate target size.

    use sharepoint_config
    go
    alter database sharepoint_config set recovery simple
    go
    dbcc shrinkfile('SharePoint_Config_log',100)
    go
    alter database sharepoint_config set recovery FUll
    go
    

    Note: 100 is called the target_size for the file in megabytes, expressed as an integer. If not specified, DBCC SHRINKFILE reduces the size to the default file size. The default size is the size specified when the file was created.

    In my humble opinion, It’s not recommended to perform the shrink operation periodically! Only in some circumstances that you need to reduce the physical size.

    You can also check this useful guide to Shrink a transaction log file Maintenance Plan in SQL Server

提交回复
热议问题